backbone.js:创建集合时定义一组初始模型
我正在创建一个主干视图,用于显示用户在我的网络应用程序中创建的文件夹列表。但我希望在列表中也显示一个像 nofolder
这样的默认条目。 我不想在视图中插入 DOM,而是只想将一个模型添加到集合中,该模型不会同步到服务器,而只是用于在视图中呈现。
我有办法做到这一点吗?我尝试过这个失败...
var def = {'name': 'none', 'selected': 'true'};
var coll = new app([def]);
// model here
var appitem = Backbone.Model.extend({
defaults: {
name: '',
id: '',
selected: 'false'
}
});
// collection here
app = Backbone.Collection.extend({
model: appitem,
url: '/apps'
});
I'm creating a backbone view for displaying a list of folders created by user in my webapp. but I want to have a default entry like no folder
to be displayed in the list as well.
Instead of inserting the DOM inside the view, I want to just add a model to the collection which does not get synced to server but is just used to be rendered in the view.
Is there a way I can do this? I tried this an failed...
var def = {'name': 'none', 'selected': 'true'};
var coll = new app([def]);
// model here
var appitem = Backbone.Model.extend({
defaults: {
name: '',
id: '',
selected: 'false'
}
});
// collection here
app = Backbone.Collection.extend({
model: appitem,
url: '/apps'
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该根据视图的需要来更改模型。
如果您需要显示“无文件夹”条目,那么它就属于视图。
不要通过向模型层添加没有意义的数据来使您的生活变得复杂。将其保留在视图中。
You should not alter your models based on what the view needs.
If you need to display a 'no folder' entry, than it belongs in the view.
Don't complicate your life by adding data without meaning to the model layer. Keep it in the view.