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!)

http://jsbin.com/oyuteb

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

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -