Julien Cochennec
Julien Cochennec

Reputation: 88

Backbone.js why can't use parent get method

something strange here, i have those two portions of code in two files, base.js and filter.js : //definition of generic object class

var baseObject = Backbone.Model.extend({
    copy: function(){
        _clipBoard=this;
    },
    fetch: function(){
        var obj =this;
        var criteriaString = encodeURI(JSON.stringify({_id:this.get("_id")}));
        var getUrl = _rootUrl+'/nalab/'+this.getMongoType()+'/';
        $.getJSON(getUrl + "_find", 'criteria=' + criteriaString, function(data){
            var results = data.results;
            obj.set(results[0]);
        });
    }
});

//definition of a baseObject subclass called filter with speical methods
var Filter = baseObject.extend({
    getTitleField : function(){
        return 'title';
    },
    getMongoType: function(){
        return 'filter';
    }
});

Then I do the following code in firefox 5 :

var filter = new Filter({_id:{$oid:"4e43ca017c36ec707b755659"}});
filter.fetch();
console.log(filter);
/*
Firebug shows an object with "attributes" that contains a field 'title'='Filtres avances'
*/
console.log(filter.get('title');
/*
Firebug says undefined
*/

So, why does "super" get method doesn't work for Filter class although it works for baseObject class and never overrid it?

Upvotes: 0

Views: 331

Answers (1)

ant_Ti
ant_Ti

Reputation: 2425

Because you try to get title before ajax request is finished and your data is set to model. If you want to get your data, try this:

var filter = new Filter({_id:{$oid:"4e43ca017c36ec707b755659"}});
filter.bind('change', function(model) {
    console.log(model.get('title');
});
filter.fetch();

Upvotes: 1

Related Questions