Problems with Backbone.Model callback and THIS

Posted by Rev. Samuel on Stack Overflow See other posts from Stack Overflow or by Rev. Samuel
Published on 2012-06-11T22:01:50Z Indexed on 2012/06/11 22:40 UTC
Read the original article Hit count: 174

Filed under:

I'm building a simple weather widget. The current weather conditions are read out of the National Weather Service xml file and then I want to parse and store the relevant data in the model but the callback for the $.ajax won't connect (the way I'm doing it).

var Weather = Backbone.Model.extend({
        initialize: function(){
            _.bindAll( this, 'update', 'startLoop', 'stopLoop' );
            this.startLoop();
        },
        startLoop: function(){
            this.update();
            this.interval = window.setInterval( _.bind( this.update, this ), 1000 * 60 * 60 );
        },
        stopLoop: function(){
            this.interval = window.clearInterval( this.interval );
        },
        store: function( data ){
            this.set({
                icon : $( data ).find( 'icon_url_name' ).text()
            });
        },
        update: function(){
            $.ajax({
                type: 'GET', 
                url: 'xml/KROC.xml', 
                datatype: 'xml' 
            })
            .done( function( data ) {
                var that = this;
                that.store( $( data ).find( 'current_observation' )[ 0 ] );
            });
        }
    });
    var weather = new Weather(); 

The data is read correctly but I can't get the done function of the call back to call the store function. (I would be happy if the "done" would just parse and then do "this.set".

Thanks in advance for your help.

© Stack Overflow or respective owner

Related posts about backbone.js