Fetching Data using .getJSON and storing using Kno

2019-09-18 10:34发布

问题:

My TweetModel is setup like this

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

[UPDATED]

Running into trouble binding the solution. For some reason, after the TweetModel object is created, it is not being pushed to self.tweets.
I broke this up into steps...

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

Any help would be appreciated, thanks.

回答1:

Try this:

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