更新/销毁我的观点,我相信我的逻辑可能需要注意
Updating/Destroying my Views, I believe my logic may need attention
我有一个骨干项目,可以从真实的API中提取播客。
我要解决的问题是删除当前显示的所有视图。我想通过将事件绑定到单击"删除"锚点时触发的每个视图来做到这一点。
目前集合重置会触发并删除所有模型,但我无法确保视图也被销毁。我想我需要逻辑方面的帮助。
这是我的代码:
podSearch = Backbone.Model.extend();
podSearchCollection = Backbone.Collection.extend({
model: podSearch,
parse: function(response) {
return response.results;
}
});
ownerList = Backbone.Model.extend();
ownerListCollection = Backbone.Collection.extend({
model: ownerList,
parse: function(response) {
return response.results;
}
});
Search = Backbone.View.extend({
initialize: function(){
this.searchCollection = new podSearchCollection();
this.searchCollection.bind('reset', this.appendResult, this);
},
events: {
"click button" : "getTerm"
},
doSearch: function(term){
/* get term and collection url */
this.searchCollection.fetch();
},
appendResult: function(){
_.each(this.searchCollection.models, function (item) {
var listItem = new ownerItem({model:item});
$('#results').append(listItem.render().el);
});
},
removeAll: function(){
console.log(this.searchCollection);
this.searchCollection.reset();
}
});
ownerItem = Backbone.View.extend({
template: $("#castOwner").html(),
initialize: function(){
this.ownerListCollection = new ownerListCollection();
this.ownerListCollection.bind('reset', this.appendResult, this);
this.model.bind("reset", this.removeSelf);
},
render: function(){
var tmpl = _.template(this.template);
this.$el.html( tmpl( this.model.toJSON() ) );
return this;
},
events: {
"click a" : "pullCasts",
"click a.removeAll" : "removeAll"
},
pullCasts: function(e){
e.preventDefault();
var id = this.$el.find('a').attr("href");
this.ownerListCollection.url = 'http://itunes.apple.com/lookup?id=' + id + '&entity=podcast&callback=?';
this.ownerListCollection.fetch();
},
appendResult: function(){
_.each(this.ownerListCollection.models, function(item){
/* do something with each item */
}, this);
$(this.el).append('<p><a href="#" class="removeAll">Remove All</a></p>');
},
removeAll: function(){
search.removeAll();
},
removeSelf: function(){
console.log("rm");
}
});
search = new Search();
为了在模型被销毁时删除视图,您可以向视图添加一个侦听器,如果模型被销毁,它将删除视图:-
initialize: function () {
//view gets re-rendered if model is changed
this.model.on("change", this.render, this);
//view gets removed if model is destroyed
this.model.on("destroy", this.remove, this)
},
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 可以't让我的if语句处理js中的html表单输入
- 我的单元测试选项是什么
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 我的职位回报太快了,如何做出承诺
- 我的jQuery插件参数没有正确启动,遇到了问题
- 如何识别我的网站中的慢速设备
- 如何将JSON数据导入我的ejs模板
- JQuery添加元素需要在我的js之前再次添加JQuery脚本
- 知道为什么我的旋转木马不会自动更改图片吗
- 我的模板未被解析
- 无法将数据从firebase获取到我的html页面
- 角度图表;t显示在我的页面中
- 我的AngularJS表达式没有'不起作用
- 将电视直播频道从网站嵌入我的网站
- /undefined在我的404错误日志中多次出现
- 为什么我的d3.jsselectAll+过滤器没有过滤
- 元素在我的代码中不会.fadeTo.Don'I don’我不知道;s错误的JavaScript、JQuery、H
- 我的javascript for循环不起作用
- 更新/销毁我的观点,我相信我的逻辑可能需要注意