javascript - 如何在环氧树脂渲染函数中仅渲染一个 View

标签 javascript jquery backbone.js underscore.js marionette

我正在开发 Backbone、Marionette、Epoxy、Underscore 程序,并且在渲染嵌套在 Render() 函数中的 View 构造函数内的单个 View 时遇到问题。该函数内部还有 3 个其他 View ,但是当调用构造函数时,我希望能够仅渲染 CustomizationView。

有什么办法可以做到这一点吗?

这是调用 SettingsView 的 View 构造函数并将其呈现在 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 中看到的 View ,它可以工作,但它会破坏程序的其余部分。有什么方法可以在调用 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")
            });

最佳答案

我解决了这个问题。我只是创建了一个名为 adminRender() 的新构造函数,然后在其中使用 this.$el.html(_.template())< 的新 html 模板调用 CustomizationView 构造函数。/。然后,当在 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();

现在可以了!!!耶:)

关于javascript - 如何在环氧树脂渲染函数中仅渲染一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39422903/

相关文章:

javascript src 无法从父级 jquery 加载中工作

javascript - 如何防止 IIFE 末尾的 "missing use strict"分配给变量?

javascript - 滚动浏览多个 DIV 元素

javascript - .替换js中的 "</div>"

javascript - 如何使用 jQuery 获取自定义标签的值

javascript - 如何构建基于 Backbone.View 的插件,以便可以单独扩展其嵌套 View ?

javascript - Backbone.js 将两个集合合二为一

javascript - jQuery.remove(),分离 DOM 元素,但我仍然可以从代码中访问这些元素。如何避免泄漏?

javascript - 将值传递给数据库

javascript - 加载模板时出现异常。下划线/ Backbone