[英]How do I access the container within reopenClass, without touching globals?
Currently building an Ember.js app using ember-cli. 目前正在使用ember-cli构建Ember.js应用。
Within a reopenClass
for a model, I need to use the Ember Data "store": 在模型的reopenClass
中,我需要使用Ember Data“存储”:
Business.reopenClass({
search: function(params) {
# nope, there is no "store" on the class/static object
var store = this.store;
}
});
I know that I can use a global, but globals are so dirty : 我知道我可以使用全局变量 ,但是全局变量太脏了 :
Business.reopenClass({
search: function(params) {
# this works, but the `window` global & `__container__` accessor are verbotten
var store = window.App.__container__.lookup("store:main");
}
});
How does one use lookup
or lookupFactory
from within a class/static function? 如何在类/静态函数中使用lookup
或lookupFactory
?
Found a solution; 找到了解决方案; following Ember's guard rails: 跟随Ember的护栏:
My Business#search method performs custom AJAX using RESTAdapter#ajax. 我的Business#search方法使用RESTAdapter#ajax执行自定义AJAX。 Appropriately moved out to the adapter, where Container#lookup is possible. 适当地移至可以进行Container#lookup的适配器。
var BusinessAdapter = ApplicationAdapter.extend({
search: function(params) {
// first class access to the container
var store = this.container.lookup("store:main");
// ...
var promise = this.ajax(url, "GET", { data: params }).then(function(adapterPayload) {
// ...
});
return promise;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.