使用Backbone.model,我想更改get方法,以便在我不将参数传递给get method
时获取对象...请参阅以下示例:
Task = Backbone.Model.extend({});
task = new Task({
id: 2,
name: "taskName"
});
task.get("id"); // 2 // ***** ok ******
task.get("name"); // "taskName" // ***** ok ******
task.get(); // undefined // ******* it should be {id: 2, name: "taskName"} *****
答案 0 :(得分:3)
您可以覆盖Model().get()
,如文档中所述:
E.g。
Task = Backbone.Model.extend({
get: function(attribute){
if(typeof attribute === "undefined"){
return this.attributes;
// Or, assuming you’ve also got json2.js available:
// return this.toJSON();
}
else {
return Backbone.Model.prototype.set.call(this, attribute);
}
}
});
答案 1 :(得分:2)