繁体   English   中英

扩展Marionette.js视图类的方法

[英]Way to extend Marionette.js view classes

我需要扩展marionette.js类,其中包含我在我的应用程序中创建的所有类中的一些功能。

我目前所做的是保存木偶的原始方法,并用我自己的方法覆盖它,从内部调用原始方法。

例如:

(function() {
  var oldMarionetteItemViewConstructor = Marionette.ItemView.prototype.constructor;
  Marionette.ItemView.prototype.constructor = function() {
     // Some custom stuff I want to have here
     .....
     // Call to original constructor
     return oldMarionetteItemViewConstructor.call(this, arguments);
  }
})();

看起来有些hacky,我想知道是否有更好的方法?

Marionette提升了Backbone.View.extend()方法(它本身实际上是从Underscore.js中提升的)所以你所要做的就是:

var MyFancyView = Marionette.ItemView.extend({

    //define your custom stuff here

});

var MyExtendedView = MyFancyView.extend({

    //This view picks up the same props/methods form MyFancyView

});

你的模式是有效的,但原生的#extend()方法会保持你的原型清洁: https//github.com/jashkenas/underscore/blob/master/underscore.js#L838

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM