![](/img/trans.png)
[英]Ember.RSVP.hash gives 'cannot convert object to primitive value' in model hook of route
[英]Model object not preserved when using hash for model in ember route
试图解决这一问题已经有一段时间了,我显然不了解模型和setupController之间的关系是如何工作的。 我有一个返回哈希值的模型; 两个查找调用的结果:
model(params) {
return Ember.RSVP.hash({
course: this.store.find('course', params.course_id),
topics: this.store.find('topic', { course_id: params.course_id })
});
},
第一次调用setupController时,会调用model
的值(如预期的那样),例如{ course: <Class>, topics: <Class> }
。 太棒了,这就是我想要的。
但是,下次调用setupController时(例如,过渡到另一条路线,然后按浏览器中的后退按钮),该model
现在只是课程<Class>
:
setupController(controller, model) {
// when first called model will be { course: <Class>, topics: <Class> }
// next time entered, model will just be <Class> (just the value of "course" )
// why is the model object not preserved?
controller.set('model', model.course);
controller.set('topics', model.topics);
}}
如果仅使model()
返回单个资源,则每次都相同:
model(params) { return this.store.find('course', params.course_id); }
// now `model` will always be "course" in setupController
为什么在使用哈希结果时不保留原始模型? 难道我做错了什么?
当您在此处链接时,您将发送模型color
:
{{#link-to 'color' color}}{{color.name}}{{/link-to}}
因此,模型挂钩不会运行。 如果将其更改为color.id,它将起作用。
在这里提到。
在上面的示例中,用于PhotoRoute的模型挂钩将使用params.photo_id = 5运行。由于已为注释段提供了模型对象,因此将不会运行用于CommentRoute的模型挂钩。 注释的ID将根据CommentRoute的序列化挂钩填充URL。
看着它,原始模型将不会保留,因为在setupController上,您正在调用controller.set('model',model.course)。 首次加载时,它会适当地调用model(params {}
函数,但在后退按钮转换和某些{{link-to}}
调用中,情况并非总是如此。
在您的setupController中,尝试将其更改为controller.set('course', model.course);
,这样您就不会在执行时也覆盖模型,并且始终可以找到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.