骨干观的继承 [英] Backbone View Inheritance

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

问题描述

我想写一个对象浏览器,其目的是在好几个地方使用不同的对象类型和略有不同的操作来实现的骨干视图。我如何能实现一些好的code重用。

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. How can I achieve some good code reuse.

我试图简单地扩展在浏览器中的骨干视图,然后扩展浏览器在我的实现然而,这给我留下了它们共同的一些属性。这是在将数据追加到所有实现与每一个浏览器建立一个不良影响。

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?

下面是一些code例子给你它目前是如何站在一个更好的主意:

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({

    });

和往常一样我比在问候我所提供的code段的任何方面欢迎反馈。

As always I more than welcome feedback in regards to any aspect of the code snippet I have provided.

感谢您,
亚历克斯

Thank you, Alex

修改
我的问题是,该子类都共享集合属性。下面是我自己的解决方案,它通过继承的方法初始化集合属性的一个例子。 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天全站免登陆