marionette - 扩展视图时扩展行为

  显示原文与译文双语对照的内容
0 0

任务是:
MyViewX使用BehaviorA和B
MyViewY扩展了 MyViewX
并使用MyViewX中的所有行为,加上 BehaviorC


//The following code works
//
var MyViewX = Marionette.ItemView.extend({
 behaviors: {
 BehaviorA: {},
 BehaviorB: {}
 }
});

var MyViewY = MyViewX.extend({
 behaviors: {
 BehaviorA: {},
 BehaviorB: {},
 BehaviorC: {}
 }
});

问题:如何利用X 中的行为定义,所以在Y 中不需要重复一个和多个。


//this is not good. It overrides X. Y has only C
var MyViewY = MyX.extend({
 behaviors: {
 BehaviorC: {}
 }
});


//maybe something like this?
//but how exactly to get X's behaviors here?
var MyViewY = MyViewX.extend({
 behaviors: _.extend{
 BEHAVIORS_OF_THE_SUPER_OF_THE_CURRENT_CLASS,
 { BehaviorC: {} }
 }
});


//this gives no errors, 
//but A and B do not behave, as if they are not used.
//
var MyViewY = MyViewX.extend({
 behaviors: { BehaviorC: {} },
 initialize: function () {
 var b = this.behaviors;
 this.behaviors = _.extend( 
 MyViewY.__super__.behaviors,
 b
 );
 }
});

时间: 原作者:

0 0

你可以通过访问它的prototype 来获得 MyViewX 行为。 使用理想的解决方案应该可以:


var MyViewY = MyViewX.extend({
 behaviors: _.extend({},
 MyViewX.prototype.behaviors,
 { BehaviorC: {} }
 );
});

因为Marionette行为被添加到 Marionette.View 构造函数的视图中,在调用末端的Backbone.View 构造函数之前,Marionette行为不工作,所以最后一个例子不起作用。

原作者:
...