2014-05-07 27 views
0

我TweetModel是设置这样获取数据使用.getJSON和存储使用Knockout.JS

function TweetModel(tweet) { 
    var self = this; 
     this.tweet = ko.observable(tweet); 
} 

[增订]

运行麻烦的结合溶液中。出于某种原因,TweetModel对象创建后,它不会被推送到self.tweets。
我打破了这种成步...

.getJSON(someurl, function(data){ 
    $.each(data, function (i, val) { 
      var tweetModel = new TweetModel(); 
      tweetModel.tweet = data.key[0]; 
      self.tweets.push(tweetModel); 
      //all could be compressed into self.tweets.push(new TweetModel(val)); 
      //Object is being created but self.tweets returns an empty list when debugged 
}} 

任何帮助,将不胜感激,谢谢。

+0

我不知道什么是 “对象被返回'对象{键:数组[1]}'” 的意思,但你尝试'$。每次(数据。 key,function(i,val){'? – Mark

+0

谢谢!做过它 –

+0

好吧,我将它添加为答案然后 – Mark

回答

0

试试这个:

$.getJSON(someurl, function (data) { 
    $.each(data.key, function (i, val) { 
     self.tweets.push(new TweetModel(val)); 
    } 
} 
+0

已更新的问题 –