1

我正在尝试使用 Ember + Rails 做一个简单的 CRUD 应用程序,但在尝试前往/workouts路线时出现以下错误。

Error while loading route: TypeError {} ember.js?body=1:415
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' 

我的代码位于:https ://github.com/ecl1pse/ember-workouts

我究竟做错了什么?

编辑:经过进一步调查,我认为罪魁祸首是

EmberWorkouts.WorkoutsRoute = Ember.Route.extend(
  model: -> EmberWorkouts.Workout.find()

这实际上并没有返回任何东西。我如何从那里调试?

如果我用这个替换它

EmberWorkouts.WorkoutsRoute = Ember.Route.extend
  model: -> [{title: 'hi'}, {title: 'damn'}]

视图实际上呈现内容。

如何让模型正确地从 Rails 收集?

4

2 回答 2

2

Ember Data 的界面在当前版本中发生了一些变化:

  1. 您可以完全清除store.js文件。Ember Data 将使用 REST 适配器自动为您设置数据存储(除非您另有说明)。
  2. 改为使用model: -> @store.find('workout')

我用您的应用程序对此进行了测试,并且可以正常工作。

如果您在过去一两周内没有通读Ember 数据指南(它发生了很大变化),我会花几分钟时间阅读。

于 2013-09-11T12:45:51.260 回答
1

对我来说,这个错误的修复(从 ember-data 1.0.0.beta.6 开始)是确保从服务器返回的 JSON 包含每个模型的“id”字段,但不要在何时显式声明 id设置 Ember DS.Model。

jbuilder模板:

json.scans do
  json.array! @scans do |scan|
    json.id scan.id # This prop has to be there
    json.name scan.name
  end
end

灰烬模型:

EmberApp.Scan = DS.Model.extend(
  // Don't include the id prop here
  name: DS.attr("string")
)
于 2014-02-20T20:19:13.837 回答