模型不刷新与淘汰赛
问题描述:
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设置为模型反映为空数组[]
任何想法有什么不对?
答
没有任何需要使用ko.computed,因为它的工作方式不同。你需要做的只是指定任何事件处理程序并在那里填写数据。类似的东西:
某处HTML:
<button data-bind="click:getData">Get</button>
在JS:
function getData()
{
$.getJSON("http://search.twitter.com/search.json?q=%23" + myModel.tweetSearchKeyWord()+"&callback=?", function (data) {
myModel.currentTweets(data.results);
});
}
或者,如果你要更新一定的时间间隔后的数据,使用的setTimeout()JavaScript函数。
你在调试吗?而是看看'myModel.currentTweets()' – 2013-02-24 00:35:30