最佳实践UI增强添加到多个骨干木偶意见 [英] Best Practice to add UI enhancements to multiple Backbone Marionette views
问题描述
于是我就什么应用程序中的所有视图的最佳方法有一个元素上执行的操作。
So I was what the best way for all views in an application to have actions performed on an element.
在您将运行一个非单页的应用程序说:
In a non single page application you would run say:
$(document).ready(function() {
$('.autosize').autosize();
});
要自动调整功能,适用于与每一页上的自动调整类的所有元素。
to apply autosize function to all elements with the autosize class on every page.
现在在骨干木偶应用程序要做到这一点,你可以用 onDomRefresh
或类似的但会影响90%的意见你想要这样的事情执行每个视图自动运行不知何故
Now in a Backbone Marionette app to do this you could perform that in each view with onDomRefresh
or similar but for things that affect 90% of views you'd want this to run automatically somehow.
我不认为有一种方式,应用程序对象可以收听到这将有可能解决一切 onDomRefresh
事件。我认为超载 Marionette.MonitorDOMRefresh
添加这,但它不觉得自己像一个主干的做法。
I don't think there's a way that an Application object can listen to all onDomRefresh
events which would potentially solve it. I've consider overloading Marionette.MonitorDOMRefresh
to add this in but it doesn't feel like a Backbone approach.
我考虑其他的事情子类每个木偶意见添加混入加载UI元素的不同群体。
Other things I considered were sub-classing each of the marionette views to add mixins for loading different groups of UI elements.
我想其他人一定都经历过这种情况,以便有兴趣曾经用过的方法。
I figured other people must have experienced this scenario so was interested what approaches have been used.
推荐答案
在CoffeeScript的我想你也可以这样做:
In CoffeeScript I think you could also do:
extend = (obj, mixin) ->
obj[name] = method for name, method of mixin
obj
include = (klass, mixin) ->
extend klass.prototype, mixin
include Marionette.View,
onDomRefresh: () -> @$('.autosize').autosize()
这将覆盖所有的视图类型。没有测试具体而言,只是做了一些非常相似功能添加到木偶的布局视图。扩展/包括 http://arcturo.github.io/library/coffeescript/03_classes格局。 HTML 。当然,这都应该是直线上升JS太可行的。
Which should cover all the view types. Haven't tested this specifically, but just did something very similar to add functionality to Marionette's Layout view. Extend / include pattern at http://arcturo.github.io/library/coffeescript/03_classes.html. Of course this should all be doable in straight up JS too.
更新:
事实上,因为我们有下划线提供给我们,我们不需要手动定义包含
和延长
方法。我们只能说:
Actually, since we have Underscore available to us we don't need to manually define the include
and extend
methods. We can just say:
_.extend Marionette.View.prototype,
onDomRefresh: () -> @$('.autosize').autosize()
这篇关于最佳实践UI增强添加到多个骨干木偶意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!