问题描述:

So I have:

  1. Class MySuperBehavior extends Marionette.Behavior
  2. Bunch of classes extends Marionette.ItemView and uses MySuperBehavior, like tralalaView, trololoView etc

I want to create method into MySuperBehavior which every instance of TralalaView and TrololoView can use. How can I do it correctly?

Sample code to illustrate:

 var mySuperBehavior = Marionette.Behavior.extend({

awesomeNonStaticMethod: function(){

console.log(this);

}

});

We suppose correct linking of classes

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

网友答案:

I did like that:

 var mySuperBehavior =  Marionette.Behavior.extend({
        initialize: function(options, view){
            this.view.awesomeNonStaticMethod = this.awesomeNonStaticMethod
        }
        awesomeNonStaticMethod: function(){
            console.log(this);
        }
    });
相关阅读:
Top