从 Backbone.View 中的 collection 中删除单个模型
Delete single model from collection in Backbone.View
var PlayersView = Backbone.View.extend({
collection: players, //collection of players
el: "#playersList", //I bind to the class
render: function () {
this.$el.html("");
for (var i=0; i<this.collection.size(); i++) {
var player = this.collection.at(i);
this.$el.append("<li "+"value='"+player.get("id")+"'>"+player.get("names")[0]+" "+player.get("surnames")[0]+" <a href='#' class='edit'>[edit]</a>"+"</li>");
} //what I render is in the picture below
return this;
},
events: {
'click .edit': 'edit',
},
edit: function () {
console.log(???); //what to write?
}
});
嘿,我渲染了整个 collection 玩家,看起来像每个玩家的姓名列表和 [edit] 按钮。当我点击编辑按钮时,如何从我的 collection 那个按钮旁边的确切播放器中获取模型?有没有简单的方法,或者我必须获得包含其 ID 的 parents 字段 "value",然后在我的 collection?
中查找此 ID
您必须在集合中搜索模型的 ID。
请注意,backbone 会将代表点击事件的对象作为第一个参数传递给您的 edit
函数。单击事件的 target
属性将是被单击的元素。为编辑 link 提供一个与您模型的 ID 匹配的 ID 属性 可能是最简单的方法。
var PlayersView = Backbone.View.extend({
collection: players, //collection of players
el: "#playersList", //I bind to the class
render: function () {
this.$el.html("");
for (var i=0; i<this.collection.size(); i++) {
var player = this.collection.at(i);
this.$el.append("<li "+"value='"+player.get("id")+"'>"+player.get("names")[0]+" "+player.get("surnames")[0]+" <a href='#' id='" + player.get("id") + "' class='edit'>[edit]</a>"+"</li>");
} //what I render is in the picture below
return this;
},
events: {
'click .edit': 'edit',
},
edit: function (e) {
this.collection.get(e.target.get("id"))
}
});
var PlayersView = Backbone.View.extend({
collection: players, //collection of players
el: "#playersList", //I bind to the class
render: function () {
this.$el.html("");
for (var i=0; i<this.collection.size(); i++) {
var player = this.collection.at(i);
this.$el.append("<li "+"value='"+player.get("id")+"'>"+player.get("names")[0]+" "+player.get("surnames")[0]+" <a href='#' class='edit'>[edit]</a>"+"</li>");
} //what I render is in the picture below
return this;
},
events: {
'click .edit': 'edit',
},
edit: function () {
console.log(???); //what to write?
}
});
嘿,我渲染了整个 collection 玩家,看起来像每个玩家的姓名列表和 [edit] 按钮。当我点击编辑按钮时,如何从我的 collection 那个按钮旁边的确切播放器中获取模型?有没有简单的方法,或者我必须获得包含其 ID 的 parents 字段 "value",然后在我的 collection?
中查找此 ID您必须在集合中搜索模型的 ID。
请注意,backbone 会将代表点击事件的对象作为第一个参数传递给您的 edit
函数。单击事件的 target
属性将是被单击的元素。为编辑 link 提供一个与您模型的 ID 匹配的 ID 属性 可能是最简单的方法。
var PlayersView = Backbone.View.extend({
collection: players, //collection of players
el: "#playersList", //I bind to the class
render: function () {
this.$el.html("");
for (var i=0; i<this.collection.size(); i++) {
var player = this.collection.at(i);
this.$el.append("<li "+"value='"+player.get("id")+"'>"+player.get("names")[0]+" "+player.get("surnames")[0]+" <a href='#' id='" + player.get("id") + "' class='edit'>[edit]</a>"+"</li>");
} //what I render is in the picture below
return this;
},
events: {
'click .edit': 'edit',
},
edit: function (e) {
this.collection.get(e.target.get("id"))
}
});