簡體   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