所以我有:
MySuperBehavior
延伸Marionette.Behavior
Marionette.ItemView
并使用MySuperBehavior
,例如tralalaView
,trololoView
等我想在MySuperBehavior中创建方法,TralalaView和TrololoView的每个实例都可以使用它。我怎么能正确地做到这一点?
示例代码说明:
var mySuperBehavior = Marionette.Behavior.extend({
awesomeNonStaticMethod: function(){
console.log(this);
}
});
我们假设正确的类链接
var tralalaView = Marionette.ItemView.extend({
behaviors: {
somebehavior: {
behaviorClass: MySuperBehavior
}
}
// some methods here
});
var instOfTralala = new tralalaView();
console.log(instOfTralala.awesomeNonStaticMethod()); // I want something like this
答案 0 :(得分:1)
我确实喜欢这样:
var mySuperBehavior = Marionette.Behavior.extend({
initialize: function(options, view){
this.view.awesomeNonStaticMethod = this.awesomeNonStaticMethod
}
awesomeNonStaticMethod: function(){
console.log(this);
}
});