0

我正在为如何用 Knockout 最好地表示我的数据而苦苦挣扎。拥有一个包含子视图模型的主视图模型还是只有一个主视图模型更好?以下是表示数据的方式的示例:

Company { CompanyID, Industry, Name, Address, Phone }

Employee { CompanyID, EmployeeID, Name, Address, Phone }

可以有很多公司,每个公司可以有很多员工。我需要用户能够从下拉列表中选择一家公司,填写公司记录,然后显示员工记录列表。如果公司记录发生更改,我只想将公司记录(通过 jSON)发回服务器。如果员工记录发生变化,我只想将员工记录发送回服务器。

当然,在Employee加载记录之前不能加载任何记录Company

4

1 回答 1

1

Try not to think about "better" ... instead think about what your View needs. Build a viewmodel for it with the models that it needs. if you need a list of companies, to select a company, then to show a list of employees all in one View then you could have a viewmodel which exposes this hierarchy of models.

Or you could create multiple Views: 1 for selecting the company from a list, 1: for show company details, 1: for selecting employees ... you get the idea. Then each view has its own viewmodel with that data.

Or you can create a master viewmodel with sub viewmodels.

I usualyl start with 1 View and 1 ViewModel and only split amongst several once I get to a point where it feels like the ViewModel (or view) is getting heavy.

于 2012-08-28T19:15:14.793 回答