Thursday, August 30, 2012

Ext.data.Model.load doesn't use the idProperty of the current model


When you create you own model by extending 'Ext.data.Model', you can use the function load which take an identity as first parameter and an configuration object as second. The problem is ExtJs 4.1 doesn't use the idProperty of the new model to send the parameter but 'id'.
You can override the static function load from Ext.data.Model to modify this behavior:

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

       var me = this ,
            params={};
      params[me.prototype.idProperty] = 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: