Ember.js 模型中的保留属性名称 [英] Reserved attribute names in Ember.js models

查看:27
本文介绍了Ember.js 模型中的保留属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下字段的 Post 模型:

I have a Post model with the following fields:

BlogApp.Post = DS.Model.extend({
    author: DS.attr('string'),
    title: DS.attr('string'),
    preamble: DS.attr('string'),
    content: DS.attr('string'),
    created: DS.attr('date'),
    comments: DS.hasMany('BlogApp.Comment'),
    lastUpdate: DS.attr('date')
});

渲染后,不是Post.content,结果如下:

After rendering, instead of the Post.content, the result is like:

<BlogApp.Post:ember272:1>

其他字段渲染正常.我猜 content 与某些内部属性冲突.我有几个问题:

Other fields rendering are OK. I guess content is conflicting with some internal property. I have a few questions:

  1. 当 REST API 的名称与 Ember 内部结构冲突时,是否有任何解决方法?
  2. 还有其他我应该注意的禁用属性名称吗?

[更新]

名称冲突与控制器有关,与模型无关.这不是一个明确的列表,但要注意其他常见的数据库列名称,例如:

The name clash is with the controller, not with the model. It is not a definitive list but watch out for other common database column names like:

  • 模型
  • 交易
  • is_new
  • is_deleted
  • 商店
  • 错误

我猜 Ember 开发人员不像这个可怜的 Python 专家那样害怕与命名空间相关的错误.

I guess Ember developers not as afraid of namespace-related bugs as this poor pythonist.

顺便说一句,Angular.js 伙计们说得对:他们总是在 API 属性和方法前面加上 $ 有效防止此类错误.

BTW, Angular.js guys got it right: they always prefix API attributes and methods with $ effectively preventing this kind of bug.

推荐答案

我想回答你的两个问题:

I'm trying to answer your two questions:

1您可以为所有键定义映射,这里是 content 属性的示例

1 you can define a mapping for all of you Keys, here an example for the content property

App.Adapter.map('App.Post', {
  myContent: {key: 'content'}
});

2据我所知,ember 中没有这样的 explicit 保留名称列表,但根据经验,应该避免使用非常通用的名称,例如 content(预防性)

2 as far I know there is no such explicit list of reserved names in ember, but as a rule of thumb very generic names like content should be avoided (preventively)

希望能帮到你

这篇关于Ember.js 模型中的保留属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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