Sunday, December 30, 2012

Ext.data.Model.load doesn't take car of 'idProperty' of the model


The problem can appear with extended store, the 'load' function doesn't take care of 'idProperty' define in your model. You have to override 'load' method but be careful to look at mapping property of the identity field!

load: function (id, config){
       config = Ext.apply({}, config);

        var me = this ,
              params={},
              fields = me.getFields(),
              nameProperty = me.nameProperty,
              field,
              fieldName,
              i;
       
        // get field of idProperty
        for (i=fields.length-1;i>=0;i--){
               if (fields[i].name == me.prototype.idProperty){
                     field = fields[i];
                      break ;
              }
       }
        // get the field name
        if (field){
              fieldName = field[nameProperty] || field.mapping || field.name;
       } else {
              fieldName = me.prototype.idProperty;
       }
        // set identity param
       params[fieldName] = id;

       config = Ext.applyIf(config, {
              action: 'read',
              params: params
       });

        var operation = Ext.create('Ext.data.Operation' , config),
              scope = config.scope || me,
              record,
              callback;

       callback = function (operation) {
               if (operation.wasSuccessful()) {
                     record = operation.getRecords()[0];
                     Ext.callback(config.success, scope, [record, operation]);
              } else {
                     Ext.callback(config.failure, scope, [record, operation]);
              }
              Ext.callback(config.callback, scope, [record, operation]);
       };

       me.proxy.read(operation, callback, me);
}

No comments: