简体   繁体   English

在灰烬路线中将哈希用于模型时未保留模型对象

[英]Model object not preserved when using hash for model in ember route

EDIT: I've set up an actual repro of the issue on JSBIN 编辑:我已经在JSBIN上设置了该问题的实际再现

Been trying to resolve this for a while now and I'm clearly not understanding how the relationship between model and setupController works. 试图解决这一问题已经有一段时间了,我显然不了解模型和setupController之间的关系是如何工作的。 I have a model which is returning a hash; 我有一个返回哈希值的模型; the result of two find calls: 两个查找调用的结果:

model(params) {
  return Ember.RSVP.hash({
    course: this.store.find('course', params.course_id),
    topics: this.store.find('topic', { course_id: params.course_id })
  });
},

The first time setupController gets called, the value of model if as expected, a hash like { course: <Class>, topics: <Class> } . 第一次调用setupController时,会调用model的值(如预期的那样),例如{ course: <Class>, topics: <Class> } Awesome, that's what I want. 太棒了,这就是我想要的。

However, the next time setupController gets called (for example, transition to another route and then press the back button in the browser), the model is now just the course <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);
}}

If I just make model() return a single resource, it's the same every time: 如果仅使model()返回单个资源,则每次都相同:

model(params) { return this.store.find('course', params.course_id); }
// now `model` will always be "course" in setupController

Why is the original model not preserved when using a hash result? 为什么在使用哈希结果时不保留原始模型? Am I doing something wrong? 难道我做错了什么?

You're sending the model color when you're linking here: 当您在此处链接时,您将发送模型color

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

Because of that, the model hooks aren't run. 因此,模型挂钩不会运行。 If you change that to color.id, it'll work. 如果将其更改为color.id,它将起作用。

It's mentioned 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. 在上面的示例中,用于PhotoRoute的模型挂钩将使用params.photo_id = 5运行。由于已为注释段提供了模型对象,因此将不会运行用于CommentRoute的模型挂钩。 The comment's id will populate the url according to CommentRoute's serialize hook. 注释的ID将根据CommentRoute的序列化挂钩填充URL。

Looking at it, the original model will not be preserved because on setupController, you are calling controller.set('model', model.course). 看着它,原始模型将不会保留,因为在setupController上,您正在调用controller.set('model',model.course)。 When it first loads, its called the model(params {} function appropriately, but on back button transitions and certain {{link-to}} calls, that isn't always the case. 首次加载时,它会适当地调用model(params {}函数,但在后退按钮转换和某些{{link-to}}调用中,情况并非总是如此。

In your setupController, try changing it to controller.set('course', model.course); 在您的setupController中,尝试将其更改为controller.set('course', model.course); , that way you aren't overwriting your model on execution as well and it will always be able to find it. ,这样您就不会在执行时也覆盖模型,并且始终可以找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM