如何在不接触全局变量的情况下访问reopenClass中的容器?

时间:2014-06-10 19:33:28

标签: javascript ember.js ember-cli

目前正在使用ember-cli构建Ember.js应用程序。

在模型的reopenClass范围内,我需要使用Ember数据"存储":

Business.reopenClass({

  search: function(params) {
    # nope, there is no "store" on the class/static object
    var store = this.store;
  }

});

我知道我可以使用全局,但全局变量非常脏

Business.reopenClass({

  search: function(params) {
    # this works, but the `window` global & `__container__` accessor are verbotten
    var store = window.App.__container__.lookup("store:main");
  }

});

如何在类/静态函数中使用lookuplookupFactory

1 个答案:

答案 0 :(得分:1)

找到解决方案;跟随Ember的护栏:

My Business#搜索方法使用RESTAdapter #ajax执行自定义AJAX。适当地移出到适配器,可以进行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;
  }

});
相关问题