简体   繁体   English

灰烬-嵌套子路线在刷新/重新加载时丢失模型

[英]Ember - Nested child route loses model upon route refresh/reload

I have a nested route, which when refreshed loses the model and displays blank page. 我有一个嵌套的路由,刷新后会丢失模型并显示空白页面。 Code as below: 代码如下:

Router : 路由器

app.Router.map(function(){
    this.resource('main',{path:''}, function(){
        this.route('summary',{path:'/main/summary'},function(){
            this.route('details',{path:'/details'})
        })
    })
})

File structure : 文件结构

-app
 -route
  -main-route.js
  -main-summary-route.js
  -main-summary-index-route.js
  -main-summary-details-route.js
  -main-loading-route.js

 -controller
  -main-controller.js
  -main-summary-controller.js
  -main-summary-index-controller.js
  -main-summary-details-controller.js

 -templates
  -main
   -summary
    -index.hbs
    -details.bhs
   -summary.hbs
   -loading.hbs
  -main.hbs
  -application.hbs

Brief about code : Templates main.hbs and application.hbs have {{outlet}} defined in them. 关于代码的简要说明 :模板main.hbsapplication.hbs中定义了{{outlet}}。 summary.hbs has {{outlet}} in it as well, so that when url /main/summary is hit, it shows only contents from summary/index.hbs and when url /main/summary/details is hit, it only shows the one in details by rendering into summary. summary.hbs中也有{{outlet}},因此当点击url / main / summary时,它仅显示summary / index.hbs中的内容;当点击url / main / summary / details时,它仅显示通过渲染成摘要来详细介绍一个。

My ajax call goes in model hook of " main-summary-route ", and while its waiting, i show loading template. 我的ajax调用进入“ main-summary-route ”的模型钩子中,在等待时,我显示加载模板。 " main-summary-details-controller.js " extends from " main-summary-index-controller.js " so that code could be reused. main-summary-details-controller.js ”是从“ main-summary-index-controller.js ”扩展而来的,因此可以重复使用代码。 Similarly, " main-summary-details-route.js " gets the same model as the one in " main-summary-route.js " via model hook in details route as - 同样,“ main-summary-details -route.js ”通过详细信息路由中的模型钩子获得与“ main-summary-route.js ”中相同的模型-

model: function(){
    return this.modelFor('mainSummary')
}

This is because the ajax call brings together the data for both summary and routes together. 这是因为ajax调用将汇总和路由的数据汇总在一起。

Problem statement : When I hit url main/summary, i get the page and then from there, on a click, goto main/summary/details , i see the page updated with details as well, but when i refresh this (/main/summary/details) manually in browser, the page is blank, i observe that there is no model returned in model hook in details route this time. 问题陈述 :当我点击url main / summary时,我得到该页面,然后从那里单击,转到main / summary / details,我也看到页面也更新了详细信息,但是当我刷新此页面时(/ main /摘要/详细信息)在浏览器中手动进行,该页面为空白,这一次我观察到详细信息路由中的模型挂钩中没有返回任何模型。

My thoughts on solution : I thought that this would work ideally, since on refresh, it would ask for summary route first (being in parent child relation), call ajax (loading alongside) and when data comes through, it would show details. 我对解决方案的想法 :我认为这将是理想的方法,因为在刷新时,它将首先请求汇总路由(处于父子关系中),调用ajax(并排加载),并且当数据通过时,它将显示详细信息。 But i guess thats not happening. 但是我想那没有发生。

I am not getting it, as to whats going wrong. 关于出了什么问题,我不明白。 The thought which comes to my mind is that, do i need to probably catch the refresh event in details route and call ajax again, so that it fetches data. 我想到的是,是否可能需要在详细信息路由中捕获刷新事件并再次调用ajax,以便它获取数据。

Ember version used - 1.13.0 使用的Ember版本-1.13.0

I did solve this by eventually calling ajax again on refreshing the details page. 我确实通过最终在刷新详细信息页面上再次调用ajax来解决了这个问题。 I did place a check though, that if "this.modelFor('mainSummary')" is available then return this model only, otherwise get model from ajax. 我确实进行了检查,如果“ this.modelFor('mainSummary')”可用,则仅返回此模型,否则从ajax获取模型。 This worked for me. 这对我有用。

But I am still wondering, that ideally it should have been by itself, ie the as i refresh, the ajax on /main/summary should have been called itself and thereby rendering details, once model was available. 但是我仍然想知道,理想情况下应该是它本身,即当我刷新时,一旦模型可用,就应该调用/ main / summary上的ajax并因此呈现细节。

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

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