camelCased型号名称 [英] camelCased Model names

查看:147
本文介绍了camelCased型号名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道骆驼类属性最近有没有问题。

does anyone know if there is a recent problem with camelcased properties.

我有一个这样的模型:

var attr = DS.attr;

App.Users = DS.Model.extend({
    firstName: attr('string'),
    phone: attr('string'),
    email: attr('string')
});

在我的模板电子邮件和手机正确显示,但firstName没有出现。我检查了json文件,一切似乎都很好。所有我的其他模型都出现相同的问题,
,所以我想它必须用camelCase做一些事情。

In my template email and phone show up correctly but firstName doesnt appear. I checked the json file and everything seems to be fine. With all my other models the same problems appear, so i guess it has to do something with the camelCase.

推荐答案

弃权通知:这个答案是从2013年开始,直到现在还没有触及。这不反映JSON API适配器,而是Ember-Data的RESTAdapter。

Ember.js依赖于命名约定,它期望多字 - 骆驼案(例如 firstName )模型属性映射到JSON响应的多字下划线分隔小写属性(例如 first_name )。如果您的JSON为您提供 firstName ,或者您在控制后端API的场景中不符合本惯例的任何内容,则您将定义一个映射的选项,告诉您的适配器在JSON响应中查找特定的键,并将其映射到给定模型中的属性。

Ember.js relies on naming conventions, and it expects that multiple-word-camel-case (e.g. firstName) model properties to be mapped to multiple-word-underscore-separated-lower-case attributes (e.g. first_name) on the JSON response. If your JSON is giving you firstName, or anything that is not in this convention in a scenario that you do not control the backend API, you have the option of defining a map which tells your adapter to look for a specific key in the JSON response and map it to a property in a given Model.

你可以这样做:

DS.RESTAdapter.map('App.User', { 
    firstName: { key: 'firstName' },
    lastName: { key: 'familyName' }
});

请注意,在上面的示例中,我添加了一个 lastName 属性不属于您自己的模型。我已经做到这一点,只是为了说明你可以同时映射几个属性,它的JSON响应中的名称可以是任何东西,不同的名称在不同的外壳。

Note that in the sample above I've added a lastName property which is not part of your own model. I've done that just to make it clear that you can map several properties at the same time, and its name in the JSON response can be anything, not necessarily the same name in a different casing.

这篇关于camelCased型号名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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