编辑:问题的jsbin。请参阅 IndexController 的 testAddTom 操作方法中的注释。
我有一个控制器,可以将模型对象添加到我的应用程序中的另一个 ArrayController。但是,当我在 ArrayController 上调用 pushObject 时,会引发错误...
未捕获的类型错误:无法调用未定义的 ember-data-latest.js:2769 DS.Store.Ember.Object.extend.materializeRecord ember-data-latest.js:2769 DS.Store.Ember.Object.extend 的方法“_create”。 recordForReference ember-data-latest.js:1871 DS.RecordArray.Ember.ArrayProxy.extend.objectAtContent ember-data-latest.js:309 superWrapper ember.js:1044 Ember.ArrayProxy.Ember.Object.extend.objectAt ember.js :11475 superWrapper ember.js:1044 Ember.ArrayController.Ember.ArrayProxy.extend.objectAtContent ember.js:13111 superWrapper ember.js:1044 Ember.ArrayProxy.Ember.Object.extend.objectAt ember.js:11475 superWrapper ember.js :1044 Ember.Array.Ember.Mixin.create.arrayContentDidChange ember.js:8955 Ember.ArrayController.Ember.ArrayProxy.extend.arrayContentDidChange ember.js:13147 superWrapper ember.js:1044 Ember.ArrayProxy。Ember.Object.extend.arrangedContentArrayDidChange ember.js:11582 sendEvent ember.js:2432 Ember.Array.Ember.Mixin.create.arrayContentDidChange ember.js:8946 Ember.ArrayProxy.Ember.Object.extend.arrangedContentArrayDidChange ember.js:11582 sendEvent ember.js:2432 Ember.Array.Ember.Mixin.create.arrayContentDidChange ember.js:8946 Ember.Mixin.create.replace ember.js:12016 Ember.ArrayProxy.Ember.Object.extend.replaceContent ember.js:11371 Ember.ArrayProxy.Ember.Object.extend._replace ember.js:11487 Ember.ArrayProxy.Ember.Object.extend.replace ember.js:11493 Ember.ArrayProxy.Ember.Object.extend.replaceContent ember.js:11371 Ember。 ArrayProxy.Ember.Object.extend._replace ember.js:11487 Ember.ArrayProxy.Ember.Object.extend._insertAt ember.js:11501 Ember.ArrayProxy.Ember.Object.extend.pushObject安排ContentArrayDidChange ember.js:11582 sendEvent ember.js:2432 Ember.Array.Ember.Mixin.create.arrayContentDidChange ember.js:8946 Ember.ArrayProxy.Ember.Object.extend.arrangedContentArrayDidChange ember.js:11582 sendEvent ember.js:2432 Ember.Array.Ember.Mixin.create.arrayContentDidChange ember.js:8946 Ember.Mixin.create.replace ember.js:12016 Ember.ArrayProxy.Ember.Object.extend.replaceContent ember.js:11371 Ember.ArrayProxy.Ember。 Object.extend._replace ember.js:11487 Ember.ArrayProxy.Ember.Object.extend.replace ember.js:11493 Ember.ArrayProxy.Ember.Object.extend.replaceContent ember.js:11371 Ember.ArrayProxy.Ember.Object。 extend._replace ember.js:11487 Ember.ArrayProxy.Ember.Object.extend._insertAt ember.js:11501 Ember.ArrayProxy.Ember.Object.extend.pushObject安排ContentArrayDidChange ember.js:11582 sendEvent ember.js:2432 Ember.Array.Ember.Mixin.create.arrayContentDidChange ember.js:8946 Ember.ArrayProxy.Ember.Object.extend.arrangedContentArrayDidChange ember.js:11582 sendEvent ember.js:2432 Ember.Array.Ember.Mixin.create.arrayContentDidChange ember.js:8946 Ember.Mixin.create.replace ember.js:12016 Ember.ArrayProxy.Ember.Object.extend.replaceContent ember.js:11371 Ember.ArrayProxy.Ember。 Object.extend._replace ember.js:11487 Ember.ArrayProxy.Ember.Object.extend.replace ember.js:11493 Ember.ArrayProxy.Ember.Object.extend.replaceContent ember.js:11371 Ember.ArrayProxy.Ember.Object。 extend._replace ember.js:11487 Ember.ArrayProxy.Ember.Object.extend._insertAt ember.js:11501 Ember.ArrayProxy.Ember.Object.extend.pushObjectArray.Ember.Mixin.create.arrayContentDidChange ember.js:8946 Ember.ArrayProxy.Ember.Object.extend.arrangedContentArrayDidChange ember.js:11582 sendEvent ember.js:2432 Ember.Array.Ember.Mixin.create.arrayContentDidChange ember.js :8946 Ember.Mixin.create.replace ember.js:12016 Ember.ArrayProxy.Ember.Object.extend.replaceContent ember.js:11371 Ember.ArrayProxy.Ember.Object.extend._replace ember.js:11487 Ember.ArrayProxy。 Ember.Object.extend.replace ember.js:11493 Ember.ArrayProxy.Ember.Object.extend.replaceContent ember.js:11371 Ember.ArrayProxy.Ember.Object.extend._replace ember.js:11487 Ember.ArrayProxy.Ember。 Object.extend._insertAt ember.js:11501 Ember.ArrayProxy.Ember.Object.extend.pushObjectArray.Ember.Mixin.create.arrayContentDidChange ember.js:8946 Ember.ArrayProxy.Ember.Object.extend.arrangedContentArrayDidChange ember.js:11582 sendEvent ember.js:2432 Ember.Array.Ember.Mixin.create.arrayContentDidChange ember.js :8946 Ember.Mixin.create.replace ember.js:12016 Ember.ArrayProxy.Ember.Object.extend.replaceContent ember.js:11371 Ember.ArrayProxy.Ember.Object.extend._replace ember.js:11487 Ember.ArrayProxy。 Ember.Object.extend.replace ember.js:11493 Ember.ArrayProxy.Ember.Object.extend.replaceContent ember.js:11371 Ember.ArrayProxy.Ember.Object.extend._replace ember.js:11487 Ember.ArrayProxy.Ember。 Object.extend._insertAt ember.js:11501 Ember.ArrayProxy.Ember.Object.extend.pushObjectjs:11582 sendEvent ember.js:2432 Ember.Array.Ember.Mixin.create.arrayContentDidChange ember.js:8946 Ember.Mixin.create.replace ember.js:12016 Ember.ArrayProxy.Ember.Object.extend.replaceContent ember. js:11371 Ember.ArrayProxy.Ember.Object.extend._replace ember.js:11487 Ember.ArrayProxy.Ember.Object.extend.replace ember.js:11493 Ember.ArrayProxy.Ember.Object.extend.replaceContent ember.js: 11371 Ember.ArrayProxy.Ember.Object.extend._replace ember.js:11487 Ember.ArrayProxy.Ember.Object.extend._insertAt ember.js:11501 Ember.ArrayProxy.Ember.Object.extend.pushObjectjs:11582 sendEvent ember.js:2432 Ember.Array.Ember.Mixin.create.arrayContentDidChange ember.js:8946 Ember.Mixin.create.replace ember.js:12016 Ember.ArrayProxy.Ember.Object.extend.replaceContent ember. js:11371 Ember.ArrayProxy.Ember.Object.extend._replace ember.js:11487 Ember.ArrayProxy.Ember.Object.extend.replace ember.js:11493 Ember.ArrayProxy.Ember.Object.extend.replaceContent ember.js: 11371 Ember.ArrayProxy.Ember.Object.extend._replace ember.js:11487 Ember.ArrayProxy.Ember.Object.extend._insertAt ember.js:11501 Ember.ArrayProxy.Ember.Object.extend.pushObject11487 Ember.ArrayProxy.Ember.Object.extend.replace ember.js:11493 Ember.ArrayProxy.Ember.Object.extend.replaceContent ember.js:11371 Ember.ArrayProxy.Ember.Object.extend._replace ember.js:11487 Ember .ArrayProxy.Ember.Object.extend._insertAt ember.js:11501 Ember.ArrayProxy.Ember.Object.extend.pushObject11487 Ember.ArrayProxy.Ember.Object.extend.replace ember.js:11493 Ember.ArrayProxy.Ember.Object.extend.replaceContent ember.js:11371 Ember.ArrayProxy.Ember.Object.extend._replace ember.js:11487 Ember .ArrayProxy.Ember.Object.extend._insertAt ember.js:11501 Ember.ArrayProxy.Ember.Object.extend.pushObject
模型对象不是新的,它早先已经获取了。如果我改为获取 ArrayController 内容,将其转换为数组,将对象推送到数组,然后重新设置 ArrayController.content 它可以正常工作。
我的问题是为什么 pushObject 不起作用?这似乎是正确的做法。
pushObject 代码示例破坏了...
App.TaskController.reopen({
needs: ['reminder'],
showReminderForTask: function(reminder) {
var reminders = this.get('controllers.reminders');
if (!reminders.content.contains(reminder)) {
reminders.pushObject(reminder);
}
}
});
有效的 ArrayController.content 示例....
App.TaskController.reopen({
needs: ['reminder'],
showReminderForTask: function(reminder) {
var reminders = this.get('controllers.reminders');
if (!reminders.content.contains(reminder)) {
var content = reminders.content.toArray();
content.push(reminder);
reminders.set('content', content);
}
}
});