首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在epoxy render函数中只渲染一个视图

如何在epoxy render函数中只渲染一个视图
EN

Stack Overflow用户
提问于 2016-09-10 13:29:20
回答 1查看 65关注 0票数 1

我正在开发一个Backbone,Marionette,Epoxy,Underscore程序,我在渲染一个嵌套在Render()函数中的视图构造函数中的单个视图时遇到了麻烦。在这个函数中还有另外3个视图,但是当构造函数被调用时,我只希望能够呈现CustomizationView。

有没有办法让我做到这一点?

这是调用SettingsView的视图构造函数并在CustomerView内部呈现它的代码

代码语言:javascript
复制
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之外,它还包含许多我需要的代码。

代码语言:javascript
复制
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")
            });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-15 11:53:53

我解决了这个问题。我简单地创建了一个名为adminRender()的新构造函数,然后在其中使用一个用于CustomizationView的新html模板调用this.$el.html(_.template())构造函数。然后,当在CustomerView中实例化SettingsView类对象时,我测试了当前工作目录,并创建了一个调用适当呈现函数的三元语句。它看起来是这样的:

代码语言:javascript
复制
            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构造函数中,我会像这样调用适当的呈现函数:

代码语言:javascript
复制
            var pathName = window.location.pathname;
            var pathName = "/admin/customers/new" ? view.adminRender() : view.userRender();

现在它起作用了!耶:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39422903

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档