Object 在Ember.js中扩展视图或使用mixin

Object 在Ember.js中扩展视图或使用mixin,object,view,ember.js,mixins,Object,View,Ember.js,Mixins,我想在一个余烬应用程序的几个视图中包括标准功能。该功能包括将视图的标记名和类名设置为相同,以及跟踪每个视图的属性 简而言之,问题是:我应该使用mixin还是扩展基本视图? 扩大的问题 是否应该扩展基础视图来执行此操作?例如: App.BaseView = Em.View.extend({ tagName: 'section', classNames: ['page_section', 'blue'], willInsertElement: function() {

我想在一个余烬应用程序的几个视图中包括标准功能。该功能包括将视图的标记名和类名设置为相同,以及跟踪每个视图的属性

简而言之,问题是:我应该使用mixin还是扩展基本视图?

扩大的问题

是否应该扩展基础视图来执行此操作?例如:

App.BaseView = Em.View.extend({
    tagName: 'section',
    classNames: ['page_section', 'blue'],
    willInsertElement: function() {
        // Some functions called here that set properties
    },
});

App.PageOneView = App.BaseView.extend({
    // View specific stuff here
});

App.PageTwoView = App.BaseView.extend({
    // View specific stuff here
});
App.BaseMixin = Em.Mixin.create({
    tagName: 'section',
    classNames: ['page_section', 'blue'],
    willInsertElement: function() {
        // Some functions called here that set properties
    },
});

App.PageOneView = Em.View.extend(App.BaseMixin, {
    // View specific stuff here
});

App.PageTwoView = Em.View.extend(App.BaseMixin, {
    // View specific stuff here
});
App.BaseMixin = Em.Mixin.create({
    tagName: null,
    classNames: null,
    init: function() {
        this.set('tagName', 'section');
        // And so forth...
    },
});
。。。或者,应该使用Mixin来扩展功能吗?例如:

App.BaseView = Em.View.extend({
    tagName: 'section',
    classNames: ['page_section', 'blue'],
    willInsertElement: function() {
        // Some functions called here that set properties
    },
});

App.PageOneView = App.BaseView.extend({
    // View specific stuff here
});

App.PageTwoView = App.BaseView.extend({
    // View specific stuff here
});
App.BaseMixin = Em.Mixin.create({
    tagName: 'section',
    classNames: ['page_section', 'blue'],
    willInsertElement: function() {
        // Some functions called here that set properties
    },
});

App.PageOneView = Em.View.extend(App.BaseMixin, {
    // View specific stuff here
});

App.PageTwoView = Em.View.extend(App.BaseMixin, {
    // View specific stuff here
});
App.BaseMixin = Em.Mixin.create({
    tagName: null,
    classNames: null,
    init: function() {
        this.set('tagName', 'section');
        // And so forth...
    },
});
我知道视图和mixin都是Ember对象,但使用它们中的任何一个将标准功能扩展到其他对象(例如视图)是否会影响对象和原型/实例(如果它们与对象不同)的交互方式,以及是否在视图的实例或视图对象上设置属性

如果上面两个例子不同,那么在mixin的init函数上设置属性会改变什么吗?例如:

App.BaseView = Em.View.extend({
    tagName: 'section',
    classNames: ['page_section', 'blue'],
    willInsertElement: function() {
        // Some functions called here that set properties
    },
});

App.PageOneView = App.BaseView.extend({
    // View specific stuff here
});

App.PageTwoView = App.BaseView.extend({
    // View specific stuff here
});
App.BaseMixin = Em.Mixin.create({
    tagName: 'section',
    classNames: ['page_section', 'blue'],
    willInsertElement: function() {
        // Some functions called here that set properties
    },
});

App.PageOneView = Em.View.extend(App.BaseMixin, {
    // View specific stuff here
});

App.PageTwoView = Em.View.extend(App.BaseMixin, {
    // View specific stuff here
});
App.BaseMixin = Em.Mixin.create({
    tagName: null,
    classNames: null,
    init: function() {
        this.set('tagName', 'section');
        // And so forth...
    },
});
但是,如果使用mixin和扩展视图对我试图添加标准功能的视图有相同的影响(即,它们以相同的方式影响视图的对象和原型/实例),那么您是否认为使用其中一个比另一个有优势(无论是在效率、可维护性等方面)?

好问题,

简短而简单,扩展视图

  • 钩子/绑定是特定于视图的,因此mixin不能应用于控制器、路由等,并且根据您的团队组成,您不希望给某人机会混合不属于您的代码

  • 在Ember中扩展一个类只会使基类成为一个mixin并将其应用到您的类中

  • 所以这几乎是完全一样的,只有基本视图更有意义,因为它只适用于视图