Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 根据不同的事件模块更改视图内容_Javascript_Backbone.js_Marionette - Fatal编程技术网

Javascript 根据不同的事件模块更改视图内容

Javascript 根据不同的事件模块更改视图内容,javascript,backbone.js,marionette,Javascript,Backbone.js,Marionette,My content box模块枚举一个集合,并为每个项目创建一个容器视图(将模型传递给视图)。它将初始内容设置为其模型的内容属性。基于模型中的布局属性,容器视图将附加到DOM。这是由“\u contentBoxCreate”方法开始的 内容框模块响应侧菜单中子项的单击。侧菜单在不同的模块中实现。sidemenu sub-click事件同时传递一个包含sub_id和一些文本内容的对象。我想从此对象获取内容并使用它更新容器视图 目前,我正在通过“_sideMenuClick”方法进行此操作。在ba

My content box模块枚举一个集合,并为每个项目创建一个容器视图(将模型传递给视图)。它将初始内容设置为其模型的内容属性。基于模型中的布局属性,容器视图将附加到DOM。这是由“\u contentBoxCreate”方法开始的

内容框模块响应侧菜单中子项的单击。侧菜单在不同的模块中实现。sidemenu sub-click事件同时传递一个包含sub_id和一些文本内容的对象。我想从此对象获取内容并使用它更新容器视图

目前,我正在通过“_sideMenuClick”方法进行此操作。在backbonejs中,是否有更新视图内容的最佳实践,因为其模型上没有更改任何数据

谢谢

W.L

更新:

更改了视图

/*
 * View
 */
var Container = Backbone.View.extend({
   initialize: function() {

 this.renderableModel = this.model; // Define renderableModel & set its initial value 

       contentbox.on('update', this.update, this);
       contentbox.on('refresh', this.reset, this); // 3rd param gives context of view

        var TemplateCache = Backbone.Marionette.TemplateCache;
        this.template = TemplateCache.get("#contentbox-container");

    },
    render: function() {
        var content = this.renderableModel.get('content').toString();
        var html = this.template({content: content});

        this.$el.html(html);//backbone element

        return this;
     },
     update: function(fn) {
        /**
         * The "update" event is broadcasted to all Container views on the page.
         * We need a way to determine if this is the container we want to update.
         * Our criteria is in the fn 
         */
        var content = fn.apply(this); //criteria match return content, else null.

        /*
         * The render method knows how to render a contentbox model
         */
    if (content !== null) {

    this.renderableModel = new Contentbox();

    this.renderableModel.set({content: content}); //add content to new contentbox model

    this.render(); //Rerender the view
      }
    },
    reset: function() {

        this.renderableModel = this.model;

        this.render(); // restore view to reflect original model
     }
  });

您为什么不使用现有的
APP.vent
进行此操作?您甚至可以通过说
APP.vent.trigger('whatever',arg1,arg2,…)
将参数传递给侦听器。我的方法可能不太聪明,但我试图使视图不直接耦合到应用程序。
/*
 * View
 */
var Container = Backbone.View.extend({
   initialize: function() {

 this.renderableModel = this.model; // Define renderableModel & set its initial value 

       contentbox.on('update', this.update, this);
       contentbox.on('refresh', this.reset, this); // 3rd param gives context of view

        var TemplateCache = Backbone.Marionette.TemplateCache;
        this.template = TemplateCache.get("#contentbox-container");

    },
    render: function() {
        var content = this.renderableModel.get('content').toString();
        var html = this.template({content: content});

        this.$el.html(html);//backbone element

        return this;
     },
     update: function(fn) {
        /**
         * The "update" event is broadcasted to all Container views on the page.
         * We need a way to determine if this is the container we want to update.
         * Our criteria is in the fn 
         */
        var content = fn.apply(this); //criteria match return content, else null.

        /*
         * The render method knows how to render a contentbox model
         */
    if (content !== null) {

    this.renderableModel = new Contentbox();

    this.renderableModel.set({content: content}); //add content to new contentbox model

    this.render(); //Rerender the view
      }
    },
    reset: function() {

        this.renderableModel = this.model;

        this.render(); // restore view to reflect original model
     }
  });