【发布时间】:2015-12-14 10:28:03
【问题描述】:
是否可以在骨干网/牵线木偶应用程序中声明一个全局函数,调用精确模块的函数?
这个问题可能听起来很奇怪,因为我个人建议不要这样做,因为使用模块很棒并且可以保持应用程序结构干净。我需要一个全局函数,因为在我的应用程序的所有单个视图/模板中,我无处不在使用一个模块/函数。函数是lang.item()。例如:
lang.item(ID, p1, p2, p3, ... )
我在任何地方都使用它,我称之为“lang”的模块。示例:
define([ 'backbone', 'lang'], function(Backbone, lang){
return Backbone.View.extend({
...
template: _.template('<%= lang.item("welcome") %> <%=username%>'),
onError: function(){
this.ui.error.html(lang.item('wrong_login'));
}
...
});
});
我总是必须包含“lang”模块,这样我才能使用它。由于我调用它并在任何地方使用它,我喜欢在需要时在任何地方使用像 lang(ITEM_ID) 这样的简单全局函数,而不必每次都包含“lang”模块。
define(['Backbone'], function(Backbone){
return Backbone.View.extend({
...
template: _.template('<%= lang("welcome") %> <%=username%>'),
onError: function(){
this.ui.error.html(lang('wrong_login'));
}
...
});
});
有人有解决办法吗?
【问题讨论】:
标签: backbone.js requirejs marionette require