后端返回JSON这样的:AngularJS。装饰模型
[
{
id: 1,
name: "the name",
countryIds: [ 1,2,3]
}, ...
]
正如你可以看到有countryIds
财产。在客户端我已经缓存的国家名称与对应的标识:
[
{
id: 1,
name: "USA"
},
{
id:2, name: "France"
}...
]
,所以检索该对象时,我只需要添加新的countries
财产,我指定国名:
obj.countries = getCountryNamesByIds(obj.countryIds);
所以我最初的对象,然后看起来是这样的:
{
id: 1,
name: "the name",
countryIds: [ 1,2,3],
countries: ["USA", "France", "England"]
}
的问题是,更新(使用PUT
法)之后,发送对象到服务器时,我不想仙d到服务器的countries
属性。我如何以更优雅的方式处理这种情况?在我看来有几个选择:
1.当发送对象到服务器只是删除所有不必要的属性(坏方法,因为如果新属性将被添加到JSON,那么我需要在控制器逻辑中也应用相同的更改);
2.不要装饰用新countries
媒体资源相关联的对象,并且暴露出一些应用程序级的方法,如
$ rootScope.getCountryNamesByIds = getCountryNamesByIds;
,然后用它的模板:
<label> Countries </label>
<div> {{getCountryNamesByIds(obj.countryIds)}} </div>
3.不要装饰物与新countries
媒体资源相关联,并创建名为指令其countryNames
将产生从国家的ID(国名作为第2点):
<div country-names country-ids="obj.countryIds"> // There will be shown country names generated from their IDs</div>
你怎么想,有什么选择更优雅?或者有什么其他方式来解决我的挑战?