使用. getjson获取数据,使用Knockout.JS存储数据

Fetching Data using .getJSON and storing using Knockout.JS

本文关键字:使用 数据 JS 存储 Knockout getjson 获取      更新时间:2023-09-26

我的TweetModel是这样设置的

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
}}

试试这个:

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