gpt4 book ai didi

javascript - 在 ember 路由中使用模型哈希时,模型对象未保留

转载 作者:行者123 更新时间:2023-11-28 13:24:15 24 4
gpt4 key购买 nike

编辑:我已经设置了一个实际的 repro of the issue on JSBIN

我已经尝试解决这个问题有一段时间了,但我显然不明白模型和 setupController 之间的关系是如何工作的。我有一个返回哈希值的模型;两次 find 调用的结果:

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)<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

为什么使用哈希结果时没有保留原始模型?我做错了什么吗?

最佳答案

当您链接到此处时,您正在发送模型颜色:

{{#link-to 'color' color}}{{color.name}}{{/link-to}}

因此,模型 Hook 不会运行。如果你将其更改为 color.id,它就会起作用。

提到了here .

In the above example, the model hook for PhotoRoute will run with params.photo_id = 5. The model hook for CommentRoute won't run since you supplied a model object for the comment segment. The comment's id will populate the url according to CommentRoute's serialize hook.

关于javascript - 在 ember 路由中使用模型哈希时,模型对象未保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30465923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com