2011-08-11 40 views
0

有點怪怪的,我有代碼這兩個部分在兩個文件中,base.js和filter.js: //定義通用對象類的Backbone.js的,爲什麼不能用父get方法

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'; 
    } 
}); 

那我在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 
*/ 

那麼,爲什麼「超級」 GET方法不適用於過濾器類的工作雖然它適用於baseObject類,從來沒有overrid呢?

回答

1

因爲您在ajax請求完成並且您的數據設置爲模型之前嘗試獲取title。 如果你想獲得你的數據,試試這個:

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