我正在用jQuery和Backbone.js第一次编写一个核对清单应用程序(新手),它允许用户创建标签和复选框项目,例如:Backbone.js将两个不同的模型添加到同一个集合
有些标签甲
[]复选框项1
有些标签乙
[]复选框项目2
我想索姆e对代码的批评,因为我觉得我做错了,它变成了意大利面,而不是使用本地存储。
首先,在视图中的初始化函数()
this.listenTo(checklist, 'add', this.renderItem);
this.listenTo(checklist, 'add', this.renderLabel);
然后,对清单项目的渲染功能之一:
renderItem: function(listItem) {
if(!addingItem) return;
listItem.id = 'item' + checklist.length;
var itemView = new ListItemView({model: listItem, id: listItem.id});
// html to append
listItem.htmlToAppend = '<input type="checkbox" id="' + listItem.id+ '"/><label for="'+ listItem.id + '">' + listItem.get('title') + '</label>';
$(this.el).find('.list').append(listItem.htmlToAppend);
listItem.existing = true;
console.log(listItem.existing);
$('[type="checkbox"]').checkboxradio(); // jQuery re-render
},
我遇到的问题是:
我想添加两种不同类型的模型(清单项目和清单标签)相同的模型(清单),并且任何一个都会触发两个“添加”事件。为了防止这种情况,我必须设置基于用户如何进入它的addingItem标志,但我觉得这是一个好办法做到这一点
我隐约知道,你可以有那些<%=% > HTML中的块来加载模型,而不是以旧式的方式在HTML上附加;我也打算使用Underscore模板(在我的index.html页面中改为使用该模板),但由于我需要为每个项目/标签模型使用不同的ID,因此我不知道如何解决
As上述问题的结果或其他一些我不知道的问题,我无法使用Backbone-localStorage来获取()清单集合并刷新它,尽管我可以看到集合存在使用Chrome Javascript console
PS完整的代码是here,我写的是基于Backbone Todo example的。
谢谢
感谢您的澄清,让我意识到我被困在一些相当微不足道的东西,但却被我自己的想法所蒙蔽。 – Gary
您的欢迎伙伴 – mystic