javascript - 未捕获的 TypeError EmberJS 初始值设定项

标签 javascript jquery ember.js

所以我一直在关注这个关于 EmberJS 身份验证的教程,当我添加该页面上显示的初始化程序时,它破坏了我的应用程序。

这是破坏应用程序的代码

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

  initialize: function(container) {
    var store = container.lookup('store:main');
    var user = App.User.find('current');

    container.lookup('controller:currentUser').set('content', user);
    container.typeInjection('controller', 'currentUser', 'controller:currentUser');
  }
});

这是发生的错误

Uncaught TypeError: Object function () {
    if (!wasApplied) {
      Class.proto(); // prepare prototype...
    }
    o_defineProperty(this, GUID_KEY, undefinedDescriptor);
    o_defineProperty(this, '_super', undefinedDescriptor);
    var m = meta(this), proto = m.proto;
    m.proto = this;
    if (initMixins) {
      // capture locally so we can clear the closed over variable
      var mixins = initMixins;
      initMixins = null;
      this.reopen.apply(this, mixins);
    }
    if (initProperties) {
      // capture locally so we can clear the closed over variable
      var props = initProperties;
      initProperties = null;

      var concatenatedProperties = this.concatenatedProperties;

      for (var i = 0, l = props.length; i < l; i++) {
        var properties = props[i];

        Ember.assert("Ember.Object.create no longer supports mixing in other definitions, use createWithMixins instead.", !(properties instanceof Ember.Mixin));

        for (var keyName in properties) {
          if (!properties.hasOwnProperty(keyName)) { continue; }

          var value = properties[keyName],
              IS_BINDING = Ember.IS_BINDING;

          if (IS_BINDING.test(keyName)) {
            var bindings = m.bindings;
            if (!bindings) {
              bindings = m.bindings = {};
            } else if (!m.hasOwnProperty('bindings')) {
              bindings = m.bindings = o_create(m.bindings);
            }
            bindings[keyName] = value;
          }

          var desc = m.descs[keyName];

          Ember.assert("Ember.Object.create no longer supports defining computed properties.", !(value instanceof Ember.ComputedProperty));
          Ember.assert("Ember.Object.create no longer supports defining methods that call _super.", !(typeof value === 'function' && value.toString().indexOf('._super') !== -1));
          Ember.assert("`actions` must be provided at extend time, not at create time, when Ember.ActionHandler is used (i.e. views, controllers & routes).", !((keyName === 'actions') && Ember.ActionHandler.detect(this)));

          if (concatenatedProperties && indexOf(concatenatedProperties, keyName) >= 0) {
            var baseValue = this[keyName];

            if (baseValue) {
              if ('function' === typeof baseValue.concat) {
                value = baseValue.concat(value);
              } else {
                value = Ember.makeArray(baseValue).concat(value);
              }
            } else {
              value = Ember.makeArray(value);
            }
          }

          if (desc) {
            desc.set(this, keyName, value);
          } else {
            if (typeof this.setUnknownProperty === 'function' && !(keyName in this)) {
              this.setUnknownProperty(keyName, value);
            } else if (MANDATORY_SETTER) {
              Ember.defineProperty(this, keyName, null, value); // setup mandatory setter
            } else {
              this[keyName] = value;
            }
          }
        }
      }
    }
    finishPartial(this, m);
    this.init.apply(this, arguments);
    m.proto = proto;
    finishChains(this);
    sendEvent(this, "init");
  } has no method 'find' 

最佳答案

在 ember-data 1.0.0-beta 中,DS.Model 子类,如您的 App.User,没有find方法。相反,您必须使用:

  • store.find('user'); 查找所有数据
  • store.find('user', id); 通过id查找
  • store.find('user', { key: name });通过查询查找

结果如下:

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

  initialize: function(container) {
    var store = container.lookup('store:main');
    var user = store.find('user','current');

    container.lookup('controller:currentUser').set('content', user);
    container.typeInjection('controller', 'currentUser', 'controller:currentUser');
  }
});

希望对你有帮助

关于javascript - 未捕获的 TypeError EmberJS 初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18581452/

相关文章:

javascript - 无法使用 Microsoft Graph Api 创建带有 'Objects' 的列表项

javascript - 游戏开发: restart the sound(<audio> element) before it ended playing

jQuery 自动完成样式适用于 Chrome 但不适用于 Firefox 或 IE

ember.js - Ember-CLI-Mirage 强制执行 JSON :API?

ember.js - Ember RSVP Promise 解析总是返回未定义的值

javascript - 仅在需要滚动条时绘制闪烁

javascript - Jquery:如何根据计数克隆 <div>?

javascript - js中如何将时间转换为时间戳

javascript - div 滚动条宽度

javascript - 实现自定义 ember-simple-auth 身份验证器