问题
If I define an entity like this:
Ext.define('MyApp.model.Entity', {
extend : 'Ext.data.Model',
fields : [ {
name : 'id',
type : 'int'
} ]
});
And then instantiate it and output its id with:
var entity = Ext.create('MyApp.model.Entity');
console.log(entity.getId());
I'm getting 0
for the output. I would expect it to be undefined
. Why is that?
回答1:
There are two likely causes.
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)
.
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).
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.
来源:https://stackoverflow.com/questions/12859324/extjs-4-instantiated-model-has-id-set-to-0