![](/img/trans.png)
[英]How should I check the Backbone.View when Backbone.Model changes
[英]Backbone.model, how should I modify the get method
我想使用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"} *****
您可以重寫Model().get()
,如文檔中所述:
例如
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);
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.