初始加载后延迟加载数据 [英] Lazy loading data after initial load

查看:33
本文介绍了初始加载后延迟加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 OpenUI5 开发人员指南关于路由的教程的第 10 部分(请参阅此处),它谈到了延迟加载.然而,本教程并未展示如何延迟加载数据.

In section 10 of the OpenUI5 Developer Guide tutorial on routing (see here), it talks about lazy loading. However the tutorial does not show how to lazy-load data.

场景:我有一个 OpenUI5 应用程序,它提供员工列表,并在详细信息页面上显示主要员工详细信息,并允许访问所选员工简历中的更多信息、项目、爱好和笔记.

Scenario: I have an OpenUI5 app that gives a list of employees and on a detail page shows the main employee details and gives access to further info, projects, hobbies, and notes from the selected employee's resume.

因为在 99.99% 的用例中,用户不希望查看爱好,所以我不希望将爱好数据与应用程序的主要模型一起加载.我根据性能问题做出这个选择.相反,我会检测用户请求查看爱好并将当前用户的爱好数据加载到模型中.

Because in 99.99% of the use cases a user will not wish to look at hobbies, I will prefer not to load hobbie data with the main model for the app. I make this choice based on performance concerns. Instead I will detect the user has requested to see hobbies and load the hobbies data for the current user into the model.

我该怎么做?

我设法发现了 onTabSelect() 管道 [提示其他困惑的人,OpenUI5 教程往往会遗漏一些内容以保持主题 - 如果您迷路了,查看其工作版本的代码很有用].在此之后,教程中的路由变得更有意义了.

I managed to discover the onTabSelect() plumbing [tip to other confused folks, the OpenUI5 tutorials tend to leave stuff out to keep on topic - it is useful to look at the code of their working versions if you get lost]. After this the routing in the tutorial makes a lot more sense.

我的问题仍然相关,但略有更改为用户单击选项卡,我即将将更多 JSON 数据加载到模型中.我应该在目标控制器的 onInit() 中还是在调用 navTo() 加载控制器之前执行此操作?由于 JSON 提取的异步性质,我可以看到一个问题,这意味着如果我使用 onInit() 我可能会在数据到达之前显示页面控件.

My question remains relevant but changes slightly to 'User clicks a tab and I am about to load further JSON data into the model. Should I do this in onInit() of the target controller or before navTo() is invoked to load the controller?' I can see an issue because of the async nature of the JSON fetch meaning if I use onInit() I could end up displaying the page controls before the data arrives.

请问最佳做法是什么?我的钱在 pre navTo() 上,尽管这是一个丑陋的模式,因为如果我有另一条通往延迟加载视图的路线,我将不得不在其他地方重复 JSON 获取.

What is the best practice please ? My money is on pre navTo() though that is an ugly pattern as I would have to repeat the JSON fetch elsewhere if I have another route to the lazy-loading view.

推荐答案

坚持例子.您可以在其 onInit 方法中将 HobbiesController 附加到您正在显示的目标的显示事件:

To stick with the example. You can attach your HobbiesController in its onInit method to the display event of the target you are displaying:

var router = this.getOwnerComponent().getRouter();
var target = router.getTarget("resumeTabHobbies");
target.attachDisplay(this.onDisplay, this);

然后您定义一个 onDisplay 方法,如果您选择相应的选项卡并在那里填充您的模型,该方法现在将被调用.

Then you define an onDisplay method which will be now called if you select the corresponding tab and populate your model there.

这篇关于初始加载后延迟加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆