ExtJS 4实例化模型的id设置为0 [英] ExtJS 4 instantiated model has id set to 0
问题描述
如果我定义这样的实体:
If I define an entity like this:
Ext.define('MyApp.model.Entity', {
extend : 'Ext.data.Model',
fields : [ {
name : 'id',
type : 'int'
} ]
});
然后实例化它并使用以下命令输出其ID:
And then instantiate it and output its id with:
var entity = Ext.create('MyApp.model.Entity');
console.log(entity.getId());
我得到的输出是0
.我希望它是undefined
.为什么会这样?
I'm getting 0
for the output. I would expect it to be undefined
. Why is that?
推荐答案
有两个可能的原因.
首先,因为您在字段中使用名称id
. Ext.data.Model
具有idProperty
配置,默认为id
,用于定义要与其他字段区别对待的字段名称. getId
方法等同于get(idProperty)
.
First, because you're using the name id
for a field. Ext.data.Model
has the idProperty
config which defaults to id
, defining the name of the field to be treated differently than the rest. The getId
method is the equivalent of get(idProperty)
.
第二,因为id
的类型是int
,在这种情况下,该字段的默认值为0(除非您使用useNull
字段配置).
Second, because the type of id
is int
, in which case the default value for the field is 0 (unless you use the useNull
field config).
我个人试图避免将id
用于模型属性,因为它倾向于与几乎所有内容发生冲突.使用recordId
之类的东西,我从来没有遇到问题.
I personally try to avoid using id
for a model property because of its tendency to collide with pretty much everything. I've never had problems using something like recordId
or something similar.
这篇关于ExtJS 4实例化模型的id设置为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!