简体   繁体   English

骨干组件可重用性

[英]Backbone component reusability

I'm trying to find best option to make Backbone views reusable. 我正在努力找到使Backbone视图可重用的最佳选择。 I goggled and found many different solutions but not sure which one suits my requirements. 我g目结舌,发现了许多不同的解决方案,但不确定哪一个符合我的要求。 Basically I'm going to have many widgets filled with real time data and I need a base component that will handle service subscriptions 基本上我会有许多小部件充满实时数据,我需要一个基本组件来处理服务订阅

Is following best solution for this problem: 是针对此问题的最佳解决方案:

App.View.Base = Backbone.View.extend({
  baseMethod: function( params ) {};
});

App.ExtendedView.Base = App.View.Base.extend({
  // new stuff here

  // overriding App.View.Base.baseMethod
  baseMethod: function( params ) {
    // overriding stuff here 
    App.View.Base.prototype.baseMethod.call(this, params); // calling super.baseMethod()
  }
});

Is there any better approach? 有没有更好的方法? or should I use mixins? 或者我应该使用mixins?

骨干视图

I might be inclined to favour composition over inheritance here, and create a spinner view, and use instances of it in other views that require spinner functionality. 我可能倾向于支持组合而不是继承,并创建一个微调器视图,并在需要微调器功能的其他视图中使用它的实例。

More info: Prefer composition over inheritance? 更多信息: 首选组合而不是继承?

The typical rule-of-thumb I use for stuff like this is if there are any immutable methods in the base class that provide a common context for all your sub-classes, then inheritance makes sense. 我用于这样的东西的典型经验法则是,如果基类中有任何不可变的方法为所有子类提供公共上下文,那么继承是有意义的。 For instance, I've created a BaseView class for my Backbone application that looks something like this: 例如,我为我的Backbone应用程序创建了一个类似于下面的BaseView类:

define(function() {
    return Backbone.View.extend({
        /**
         * show() and hide() are immutable
         */
        show : function() {
            this.beforeShow();
            this.doShow();
            this.afterShow();
        },
        hide : function() {
            this.beforeHide();
            this.doHide();
            this.afterHide();
        },
        doShow : function() {
            this.$el.show();
            this.trigger('displayComplete', {action : 'show'});
        },
        doHide : function() {
            this.$el.hide();
        },
        //Override the following to extend behavior of the view
        //before and/or after the view is shown/hidden.            
        beforeShow : function() {},
        beforeHide : function() {},
        afterShow : function() {},
        afterHide : function() {}
    });
});

This is a pretty simple example, but it has proven to make things much easier for development of my application, as my central controller object is given a common interface for showing and hiding views. 这是一个非常简单的例子,但事实证明它可以使我的应用程序开发变得更加容易,因为我的中央控制器对象被赋予了一个用于显示和隐藏视图的通用界面。 I suppose you could use composition here as well, but that requires doing an explicit extend() at runtime. 我想你也可以在这里使用组合,但这需要在运行时进行显式的extend()。 You get the same result in either case, but I just prefer to have the functionality available when I instantiate my views. 在任何一种情况下都会得到相同的结果,但我更喜欢在实例化视图时使用这些功能。

Another thought is that it really depends upon what you want to accomplish. 另一个想法是,它真的取决于你想要完成的事情。 Inheritance is much more rigid than composition, but again, it depends upon what you ultimately want to accomplish, and sometimes enforcing rigidity to maintain a context is a good thing. 继承比组合更加严格,但同样,它取决于你最终想要完成的事情,有时强化刚性来维护上下文是一件好事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM