我在网络上(SOF 和 Google)看到了很多有关此问题的问题,但到目前为止还没有明确的答案。
我有一个常见的 Ember 应用程序,带有各种视图和控制器。我的一个视图有一个实例方法,我想从静态上下文中调用它。因此在一个普通的 javascript 文件中。我应该获得对 ember 实例化的视图的引用来调用该方法吗?
几行代码来说明我的问题:
在 ApplicationView.js 中:
App.ApplicationView = Em.View.extend({
templateName: 'application',
myInstanceMethod:function () {
this.anotherInstanceMethod(some, params);
},
// ... more code
});
在 MyUtils.js 中:
var myUtils = myUtils || {
myMethod: function() {
myApplicationViewInstance.myInstanceMethod();
}
};
这是我个人对这个问题的处理方法。我正在使用 Ember.View 的“didInsertElement”在中心位置注册视图。这对于单例视图非常有效。对于非单例视图,必须开发一种更复杂的 ViewRegistry。
余烬部分
var App = Ember.Application.create({
viewRegistry : {
applicationView : null
},
});
App.ApplicationView = Ember.View.extend({
templateName : 'application',
didInsertElement : function(){
App.set("viewRegistry.applicationView", this);
}
});
在 MyUtils.js 中:
var myUtils = myUtils || {
myMethod: function() {
App.get("viewRegistry.applicationView").myInstanceMethod();
}
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)