我在卸载数据模型并在进行 API 调用时正确重新填充它们时遇到问题。
型号:
/* Model Foo */
export default DS.Model.extend({
bars: DS.hasMany('bar', { async: true })
});
/* Model Bar */
export default DS.Model.extend({
foo: DS.belongsTo('foo', { async: true, inverse: 'bars' })
});
在应用程序中的某个时刻,foo 和 bar 都从 ember 数据存储中卸载,然后从 API 调用中重新加载。就像这样:
/* Unload and reload snippet */
this.store.unloadAll('bar');
this.store.unloadAll('foo');
let bars = this.store.filter('bar', {
queryParam: x
}, function(bar) {
return x === bar.x
});
let foos = this.store.filter('foo', {
queryParam: y
}, function(foo) {
return y === bar.y
});
let self = this;
Ember.RSVP.all([foos, bars]).finally(function() {
self.controller.set('model.foos', foos);
self.controller.set('model.bars', bars);
});
问题出现在依赖于这些模型更改的计算属性中。
/* Computed property elsewhere in app */
compProp: Ember.computed('foo.bars.[]', function() {
let tmp = this.get('foo.bars'); /* <-- Error generating line */
.
.
.
})
这一行给了我以下错误:
Assertion Failed: calling set on destroyed object: <DS.PromiseManyArray:ember1995>.content = <DS.ManyArray:ember3320>
错误的堆栈跟踪:
Assertion Failed: calling set on destroyed object: <DS.PromiseManyArray:ember1014>.content = <DS.ManyArray:ember1348>
Error
at assert (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:23072:13)
at Object.assert (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:23285:34)
at Object.set (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:39281:17)
at Class.set (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:52151:26)
at ManyRelationship._updateLoadingPromise (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:152522:33)
at ManyRelationship.getRecords (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:152723:21)
at Class.get (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:152101:60)
at ComputedPropertyPrototype.get (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:34367:28)
at get (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:39184:19)
at _getPath (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:39205:13)
at Object.get (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:39180:14)
感谢您的帮助!
旁注:
- 我知道
store.filter()
已弃用。我正在从早期的 Ember 版本升级该应用程序,并使用 ember-data-filter 插件来实现临时兼容性。 - 此功能适用于 Ember 1.13。
更新
为了不让这个问题悬而未决,我一直没能找到问题所在。
对我来说,解决方案是删除所有 unloadAll()
根据需要单独通话和管理记录。
最佳答案
这可能是由被 ember 破坏的元素上发生的任何异步 set
引起的。因此,追踪起来可能很困难。我通常在 Chrome 开发者控制台中打开“在捕获异常时暂停”来查找导致问题的集
。
关于ember.js - 卸载并重新加载模型后的 EmberJS "assertion failed: calling set on destroyed object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44459692/