ExtJS 4 instantiated model has id set to 0

佐手、 提交于 2019-12-10 12:14:52

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!