簡體   English   中英

在灰燼路線中將哈希用於模型時未保留模型對象

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

編輯:我已經在JSBIN上設置了該問題的實際再現

試圖解決這一問題已經有一段時間了,我顯然不了解模型和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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM