我正在开发一个Backbone,Marionette,Epoxy,Underscore程序,我在渲染一个嵌套在Render()函数中的视图构造函数中的单个视图时遇到了麻烦。在这个函数中还有另外3个视图,但是当构造函数被调用时,我只希望能够呈现CustomizationView。
有没有办法让我做到这一点?
这是调用SettingsView的视图构造函数并在CustomerView内部呈现它的代码
render:function(){
//renders all of the settings from the settings inclusion variable
var data = this.viewModel.toJSON();
data.customer = this.model.toJSON();
this.$el.html(_.template(tmpl, data, {variable:'data'}));
this.ui_settings = new settings.SettingsView({
el: this.$('#vc-customer-settings'),
model: this.model.get("_Settings")
});
this.applyBindings();
}这是位于settingsView构造函数内部的呈现函数。我可以注释掉我不想在CustomerView中看到的视图,它可以工作,但它会破坏程序的其余部分。有没有什么方法可以让我在调用settings.SettingsView构造函数时只获取CustomizationView模型?我显然希望保留SettingsView,因为除了CustomizationView之外,它还包含许多我需要的代码。
render:function(){
this.$el.html(_.template(other_settings, {}, {variable:'args'}));
this.ui_messages = new LIB.MessageCollectionView({
el:this.$('.messages ul'),
collection:this.model.get("_Messages"),
parent: this
});
this.ui_customizations = new LIB.CustomizationCollectionView({
el:this.$('.other-settings ul'),
collection:this.model.get("_Customizations")
});
this.ui_increments = new LIB.IncrementCollectionView({
el:this.$('.increments ul'),
collection:this.model.get("_Increments")
});发布于 2016-09-15 11:53:53
我解决了这个问题。我简单地创建了一个名为adminRender()的新构造函数,然后在其中使用一个用于CustomizationView的新html模板调用this.$el.html(_.template())构造函数。然后,当在CustomerView中实例化SettingsView类对象时,我测试了当前工作目录,并创建了一个调用适当呈现函数的三元语句。它看起来是这样的:
adminRender:function(){
this.$el.html(_.template(other_settings, {}, {variable: 'args'}));
this.ui_customizations = new LIB.CustomizationCollectionView({
el:this.$('.other-settings ul'),
collection:this.model.get("_Customizations")
});
this.listenToOnce(this.model, 'change:Keymap', this.initKeymap);
this.listenTo(this.model, 'keypressed', _.bind(this.replaceButton, this));
},
userRender:function(){
this.$el.html(_.template(tmpl, {}, {variable:'args'}));
this.ui_messages = new LIB.MessageCollectionView({
el:this.$('.messages ul'),
collection:this.model.get("_Messages"),
parent: this
});
this.ui_customizations = new LIB.CustomizationCollectionView({
el:this.$('.other-settings ul'),
collection:this.model.get("_Customizations")
});
this.ui_increments = new LIB.IncrementCollectionView({
el:this.$('.increments ul'),
collection:this.model.get("_Increments")
});然后,在SettingsView构造函数中,我会像这样调用适当的呈现函数:
var pathName = window.location.pathname;
var pathName = "/admin/customers/new" ? view.adminRender() : view.userRender();现在它起作用了!耶:)
https://stackoverflow.com/questions/39422903
复制相似问题