0

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

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

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

谢谢,

WL

APP.module("contentbox", function(contentbox) {

    //Model
    var Contentbox = Backbone.Model.extend({});
    //Collection
    var Contentboxes = Backbone.Collection.extend({
        model: Contentbox,
        url: 'ajax/contentboxResponse/tojson'
    });

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

            contentbox.on('update', jQuery.proxy(this.update, this)); 
            contentbox.on('refresh', jQuery.proxy(this.render, this));

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

        },
        render: function() {

            var content = this.model.get('content').toString();
            var html = this.template({content: content});

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

            return this;
        },
        update: function(fn) {

            var content = fn.apply(this);

            if (content !== null) {

                var html = this.template({content: content});

                this.$el.html(html);
            }
        }
    });

    //collection 
    var contentboxes = new Contentboxes();

    var _sideMenuToggle = function() {
        contentbox.trigger('refresh');
    };

    var _sideMenuClick = function(sideMenu) { //view contex

        var fn = function() {
            // this fn will have the context of the view!!
            var linksub = this.model.get('linksub').toString();
            if (linksub === sideMenu.id.toString()) {
                return sideMenu.content.toString();
            }
            return null;
        };

        contentbox.trigger('update', fn);
    };

    var _contentBoxCreate = function() {
        var create = function(cboxes) {
            cboxes.each(function(model) {
                var layout = "#body-" + model.get('layout');
                var $el = jQuery(layout);
                var container = new Container({model: model});
                $el.append(container.render().$el);
            });
        };
        contentboxes.fetch({
            success: create
        });
    };


    this.on("start", function() {
        _contentBoxCreate();
    });

    this.addInitializer(function() {
        APP.vent.on('sidemenu:toggle', _sideMenuToggle);
       APP.reqres.setHandler('sidemenu:submenu', _sideMenuClick);//event and content...
                                                             //from another module        
    });

});

更新:

换了个观点...

/*
 * 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
     }
  });
4

0 回答 0