arrays - KnockoutJS/AJAX update viewmodel -
currently playing knockoutjs. trying update observable array ajax/json feed (using twitter) in example.
it seems lose scope of "this" when trying update observable array (currenttweets). i've tried adding bind various places no such luck.
the error is: uncaught typeerror: cannot call method 'push' of undefined
i'm sure doing stupid, here in action (not at!)
i've read lot knockout mapping don't feel confident enough take on yet!
so, or guidance fab.
thanks
the simplest way solve proxy viewmodel's "this" variable available inside handlers. when jquery ajax calls success handler, context different refers else.
so have
function twitterviewmodel() { var self = this; this.currenttweets = ko.observablearray([]); ... this.gettweets = function(){ $.ajax({ datatype: 'jsonp', url: 'http://search.twitter.com/search.json?callback=?&q=' + this.searchterm() + '&rpp=50', success: function (data) { $.each(data.results, function(i,tweet){ self.currenttweets.push({'keywords' : 'bugger'}); }); } }); } } you can eliminate bind calls , use self instead.
hope helps.
Comments
Post a Comment