Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 模型未刷新内部ko.computed with Knockout_Javascript_Jquery_Knockout.js - Fatal编程技术网

Javascript 模型未刷新内部ko.computed with Knockout

Javascript 模型未刷新内部ko.computed with Knockout,javascript,jquery,knockout.js,Javascript,Jquery,Knockout.js,我使用这个修改过的示例代码从twitterapi中提取一些数据,并将结果设置为viewModel var myModel = new MyViewModel(); // Handler for .ready() called. function MyViewModel(){ this.show_search = ko.observable(true); // Message initially visible this.show_player = ko.observab

我使用这个修改过的示例代码从twitterapi中提取一些数据,并将结果设置为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 );
数据接收良好,data.results显示数组[15]

但是在我用

myModel.currentTweets(theData);
myModel.currentTweets反映为空数组[]


知道怎么回事吗?

没有必要使用ko.computed,因为它的工作方式不同。您需要做的只是指定任何事件处理程序并在那里填充数据。诸如此类:

html中的某处:

<button data-bind="click:getData">Get</button>

或者,如果要在确定的时间间隔后更新数据,请使用setTimeout()JavaScript函数。

是否在调试器中查找?请看
myModel.currentTweets()
function getData()
{
    $.getJSON("http://search.twitter.com/search.json?q=%23" +            myModel.tweetSearchKeyWord()+"&callback=?", function (data) {

      myModel.currentTweets(data.results);

  });
}