主干视图继承 [英] Backbone View Inheritance

查看:16
本文介绍了主干视图继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为对象浏览器编写一个主干视图,该视图旨在在具有不同对象类型和略有不同的操作的多个地方实现.

I am trying to write a Backbone view for an object browser which is designed to be implemented in several places with different object types and slightly different operation.

我曾尝试简单地在我的浏览器中扩展主干视图,然后在我的实现中扩展浏览器,但是这给我留下了一些共享的属性.这是一种不希望的效果,因为每次创建浏览器时都会将数据附加到所有实现中.

I have tried simply extending the backbone view in my browser and then extending the browser in my implementation however this leaves me with some properties which are shared. This is an undesired effect as the data is appended to all implementations with every browser creation.

有人可以阐明解决此问题的方法或替代解决方案吗?

Could someone shed light on a way to solve this problem or perhaps an alternative solution?

以下是一些代码示例,可让您更好地了解当前的情况:

Here are some code examples to give you a better idea of how it currently stands:

    var BrowserView = Backbone.View;

    _.extend(BrowserView.prototype, Backbone.View.prototype, {
        className: 'browser',

        collections: [],

        events: {

        },

        _events:{

        },

            initialize: function () {
            this._initialize();
        },

        render: function () {
            this._render();
        },

        _initialize: function () {
            this.container = $( this.make('div', {class: 'container'} ) );

            this.$el.append(this.container);

            if ( this.options.collections ) {
                this.collections = [];

                _.each(this.options.collections, this.add, this);
            }

            _.extend(this.events, this._events);

            this.delegateEvents();
        },

        _render: function () {
            this.container.empty();

            _.each(this.collections, function (view) {
                this.container.append(view.el);

                view.render();
            }, this);
        }
    });

    BrowserView.extend = Backbone.View.extend;

    var ContactBrowserView = BrowserView.extend({

    });

编辑我的问题是子类共享集合属性.这是我自己的解决方案的一个示例,它通过继承的方法初始化集合属性.jsfiddle.net/JhZXh/3

EDIT My issue is that the sub classes are sharing the collections property. Here is an example of my own solution which initialises the collections property through an inherited method. jsfiddle.net/JhZXh/3

推荐答案

这个要点显示了一个更好的选择:https://gist.github.com/2287018

This gist shows a better alternative: https://gist.github.com/2287018

这篇关于主干视图继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆