木偶。添加通过行为查看的方法

时间:2015-07-01 20:01:03

标签: javascript marionette

所以我有:

  1. 班级MySuperBehavior延伸Marionette.Behavior
  2. 一类课程延伸Marionette.ItemView并使用MySuperBehavior,例如tralalaViewtrololoView
  3. 我想在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
    

1 个答案:

答案 0 :(得分:1)

我确实喜欢这样:

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