如何正确地将属性注入控制器

时间:2014-10-08 23:20:55

标签: ember.js ember-data

这是我最初的做法,但是我收到了错误 代码:

Ember.Application.initializer({
name: "currentUser",

initialize: function(container, application) {
    var store = container.lookup('store:main'),
    attributes = $("meta[name='current-user']").attr('content');

if(attributes){
  var object = store.push(App.User, JSON.parse(attributes)),
      user   = App.User.find(object.id),
      controller = container.lookup('controller:currentUser').set('content', user);

  container.typeInjection('controller', 'currentUser', 'controller:currentUser');
  }
 }
});

错误:

Error: Cannot inject a `controller:current-user` on other controller(s). Register the `controller:current-user` as a different type and perform the typeInjection.

所以我从

开始尝试了其他一些方法
Ember.Application.initializer({
name: "currentUser",
after: "store",

initialize: function(container, application) {

    var store, user, proxy;
    var attributes = $("meta[name='current-user']").attr('content');
    if (attributes) {
        application.deferReadiness();
        container.lookup('store:main').find('user', object.id).then(function(user) {
        application.register('user:current', user, { instantiate: false, singleton: true });
        application.inject('route', 'currentUser', 'user:current');
        application.inject('controller', 'currentUser', 'user:current');
        application.advanceReadiness();
    }, function() {
         application.advanceReadiness();
      }
    });
   }
 }
});

这不会引发任何错误,但似乎也无法正常工作

我试过的另一种方法就是这个

Ember.Application.initializer({
name: "currentUser",
after: "store",

initialize: function(container, application) {

    var store, user, proxy;
    var attributes = $("meta[name='current-user']").attr('content');
    if(attributes) {
        application.deferReadiness();
        store = container.lookup('store:main');
        proxy = App.CurrentUserObjectProxy.extend();
        container.register('user:current', proxy, {singleton: true});
        proxy = container.lookup('user:current');
        var object = store.push(App.User, JSON.parse(attributes))
        store.find('user', object.id).then(function (user) {

            proxy.set('content', user);

            application.inject('controller', 'currentUser', 'user:current');

            application.advanceReadiness();
        });
       }
      }
    });

在另一个档案中

App.CurrentUserObjectProxy = Ember.ObjectProxy.extend({
content: null
});

我要调试此问题的方法是在应用程序模板上记录currentUser的名称

{{log currentUser.firstName}}

我可以在我的ember检查器中看到用户的数据正在被正确添加,它只是没有正确设置控制器上的currentUser属性,我应该采取哪些不同的提示?

1 个答案:

答案 0 :(得分:1)

调用查找已经返回ObjectProxy,因此您只需致电find并在注射器中设置该内容。

Ember.Application.initializer({
  name: "currentUser",
  after: "ember-data",

  initialize: function(container, application) {

    var store, user, proxy;
    var attributes = {id:1, name:'foo'};

    if(attributes) {
        store = container.lookup('store:main');
        store.push('user', attributes);
        var user = store.find('user', attributes.id);
        container.register('user:current', user, {singleton: true, instantiate:false});
        application.inject('controller', 'currentUser', 'user:current');
     }
   }
 });

示例:http://emberjs.jsbin.com/OxIDiVU/1144/edit

如果您希望能够在未来某天将其换出,可以使用此处找到的模式(在我的回答中提及)Ember Data Save method, Create vs update

示例:http://emberjs.jsbin.com/OxIDiVU/1145/edit