问题描述
我们假设我的数据库中有2个表,一个用户表和一个文件夹表。它们在我的后端java代码中被两个模型(bean)匹配,并且类似于我的extjs客户端中的模型。使用viewmodel架构,我想创建一个表单
,显示用户名
和 / em> 使用 viewmodel绑定 创建的文件夹。数据库中的每个文件夹都有一个 CREATE_USER_ID
字段,其中包含创建该文件夹的用户的ID。如何加载所需的数据?Viewmodels不是直接加载数据(而不是设计为加载非数据)所有模型数据)。你有两个选择:
- 去模型(推荐)。两个子选项:
- 将
folder_amount
字段添加到客户端用户模型。在服务器端,这不一定需要添加,您可以调整您的API馈送数据到客户端,以动态添加该字段。 - 如果要保持客户端模型正确匹配他们的服务器配对,使用viewmodel与关联(参见例子,向下滚动到关联)来加载文件夹本身,虽然在UI中只显示它们的数量。请注意,您不需要加载所有文件夹字段,而只需加载其ID。
- 将
- 坚持使用自己的花式非模型方法,但是这与viewmodel绑定无关。例如,这可能是在UI中呈现用户数据时进行AJAX调用以检索文件夹数。
Let's assume that I have 2 tables in my database, a user table and a folder table. They are matched by two models (beans) in my backend java code and similarly by to models in my extjs client side. Using viewmodel architecture, I want to create a form
that shows the username
and the amount of folders they have created using viewmodel bindings. Each folder in the database has a CREATE_USER_ID
field that contains the id of the user that created the folder. How do I go about loading the required data?
Viewmodels are not designed to load data directly (and not designed to load non model data at all). You've got two options:
- Go model (recommended). Two sub-options:
- Add
folder_amount
field to your client side user model. On the server side this does not necessarily need to be added as well as you can adjust your API feeding data to client to add that field dynamically. - If you want to keep your client models exactly matching their server mates, use viewmodel in conjunction with associations (see example here, scroll down to Associations) to load folders themselves, though in the UI only show the amount of them. Mind that you don't need to load all folder fields but just their IDs.
- Add
- Stick to your own fancy non model approach, but this won't have anything to do with viewmodel bindings. This may be, for example, making an AJAX call to retrieve the number of folders when user data is rendered in the UI.
这篇关于在ExtJS中加载非模型数据的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!