PDA

View Full Version : override callParent question



ttbgwt
21 Aug 2012, 1:45 PM
My question is related to this thread: http://www.sencha.com/forum/showthread.php?153078-overriding-callParent-behavior-change-(bug-)/page3

How can I get the callParent below to call the parent of the class that I have overridden and not the overridden class?



Ext.define('MDC.patch.DataReaderJson', {
override: 'Ext.data.reader.Json',


/**
* @private
* We're just preparing the data for the superclass by pulling out the record objects we want. If a {@link #record}
* was specified we have to pull those out of the larger JSON object, which is most of what this function is doing
* @param {Object} root The JSON root node
* @return {Ext.data.Model[]} The records
*/
extractData: function(root) {
var recordName = this.record,
data = [],
length, i;


if (!recordName) {
data = root;
} else {
length = root.length;


if (!length && Ext.isObject(root)) {
length = 1;
root = [root];
}


for (i = 0; i < length; i++) {
data[i] = root[i][recordName];
}
}


return this.callParent([data]); //THIS IS CALLING THE OVERRIDDEN CLASS AND NOT THE PARENT
}
});

vietits
21 Aug 2012, 6:54 PM
FYI: Below is my solution.


...
extractData: function(root) {
var recordName = this.record,
data = [],
length, i;

if (!recordName) {
data = root;
} else {
length = root.length;

if (!length && Ext.isObject(root)) {
length = 1;
root = [root];
}

for (i = 0; i < length; i++) {
data[i] = root[i][recordName];
}
}

// return this.callParent([data]); //THIS IS CALLING THE OVERRIDDEN CLASS AND NOT THE PARENT

var method = arguments.callee;
var upLevel = 2;
do {
method = method && (method.$previous || ((method = method.$owner ? method : method.caller) && method.$owner.superclass[method.$name]));
} while (method && --upLevel > 0);

return method ? method.apply(this, [data]) : null;
}

In case you want to use this calling style in many places of your application, you could use a solution of overriding Ext.Base class as below:


Ext.define('OverrideBase', {
override: 'Ext.Base',
callSuper: function(upLevel, args){
upLevel = Ext.Number.constrain(upLevel, 1, 0xffff);
var method = this.callSuper.caller;
do {
method = (method) && (method.$previous ||
((method = method.$owner ? method : method.caller) &&
method.$owner.superclass[method.$name]));
} while(method && --upLevel > 0);
return method ? method.apply(this, args) : null;
}
});
...
extractData: function(root) {
var recordName = this.record,
data = [],
length, i;


if (!recordName) {
data = root;
} else {
length = root.length;


if (!length && Ext.isObject(root)) {
length = 1;
root = [root];
}


for (i = 0; i < length; i++) {
data[i] = root[i][recordName];
}
}


// return this.callParent([data]); //THIS IS CALLING THE OVERRIDDEN CLASS AND NOT THE PARENT
return this.callSuper(2, [data]);
}