gpt4 book ai didi

javascript - 如何使用循环创建多个 Backbone View ?

转载 作者:行者123 更新时间:2023-11-30 16:01:13 24 4
gpt4 key购买 nike

我有一个实例化多个选择 View 的方法,在这些实例中唯一真正改变的是传入的模型属性。使用循环创建这些是一个很好的解决方案实例,因为数量可能会增长或者是什么推荐?

JS

setSelects: function(model) {

if(this.sessionSelect) {
this.sessionSelect.dispose();
}
this.sessionSelect = new SearchInputsView({
options: model.get('sessions'),
name: 'sessionId'
});
this.$('label[for=sessionId]').append(this.sessionSelect.render().el);

if(this.accountSelect) {
this.accountSelect.dispose();
}
this.accountSelect = new SearchInputsView({
options: model.get('accountRestrs'),
name: 'account'
});
this.$('label[for=account]').append(this.accountSelect.render().el);

if(this.tagFifty) {
this.tagFifty.dispose();
}
this.tagFifty = new SearchInputsView({
options: model.get('tag50Restrs'),
name: 'tag50'
});
this.$('label[for=tag50]').append(this.tagFifty.render().el);
},

最佳答案

var Subviews= [ { 
subview: "tagFifty",
el:"label[for=tag50]",
model:'tag50Restrs'},
},
{ ... }
]

_.each(Subviews, function(subview) {
// switch here + use of params
});

下面是我自己的代码示例:

   renderSubview: function(options) {
this.clearScreen();
this.subviewName = options.subview;

switch(options.subview) {
case 'home':
var homeView = new HomeView();
homeView.inAnimation();
this.subview = homeView;
break;
case 'rdv':
var rdvView = new RdvView();
rdvView.inAnimation();
this.subview = rdvView;
break;
case 'destinations':
var destinationsView = new DestinationsView(options);
destinationsView.inAnimation();
this.subview = destinationsView;
break;
}
},

关于javascript - 如何使用循环创建多个 Backbone View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37710061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com