这里有些奇怪,我在两个文件中有两部分代码,base.js和filter.js: //通用对象类的定义
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
*/
那么,为什么“super”get方法对Filter类不起作用,尽管它适用于baseObject类并且从不覆盖它?
答案 0 :(得分:1)
因为您在ajax请求完成之前尝试获取title
并且您的数据设置为model。
如果您想获取数据,请尝试以下操作:
var filter = new Filter({_id:{$oid:"4e43ca017c36ec707b755659"}});
filter.bind('change', function(model) {
console.log(model.get('title');
});
filter.fetch();