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

Uncaught TypeError EmberJS initializer

本文关键字:EmberJS TypeError      更新时间:2023-09-26

所以我一直在学习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数据1.0.0-beta中,DS.Model子类(如App.User)没有find方法。相反,你必须使用:

  • store.find('user');查找所有数据
  • 按id查找store.find('user', 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');
  }
});

我希望它能帮助

相关文章: