0
IM使用本變形例中的代碼來從拉Twitter的API的一些數據,並將結果設置爲一個ViewModel模型不刷新與淘汰賽
var myModel = new MyViewModel();
// Handler for .ready() called.
function MyViewModel(){
this.show_search = ko.observable(true); // Message initially visible
this.show_player = ko.observable(false); // Message initially visible
this.tweetSearchKeyWord = ko.observable("google");
this.currentTweets = ko.observableArray([]);
this.showSearch = function(){
this.show_search(true);
this.show_player(false);
};
this.showPlayer = function(){
this.show_search(false);
this.show_player(true);
};
};
ko.computed(function() {
$.getJSON("http://search.twitter.com/search.json?q=%23" + myModel.tweetSearchKeyWord()+"&callback=?", function (data) {
theData = data.results;
myModel.currentTweets(theData);
});
}, viewModel);
ko.applyBindings(myModel);
數據被收到細內部ko.computed,和data.results顯示陣列[15]
但i之後它與
myModel.currentTweets(theData);
myModel.currentTweets設置爲模型反映爲空數組[]
任何想法有什麼不對?
你在調試嗎?而是看看'myModel.currentTweets()' – 2013-02-24 00:35:30