Sorry, I did not do a sufficient job with my post - more specifically, the reason I cannot use 'this' inside my helper method is because the shared method is actually a button handler, so this references the button component, NOT the page component like below. I want to put limitedUniqueId outside cloneHandler (like firstUniqueId), but I do not know how to reference it from within cloneHandler.
Code:
Ext.define('MyHelperMixin', {
firstUniqueId: function (cfg) {
var j = 0;
var num = j;
var newItmId, i, matched;
var clones = cfg.clones;
var cloneCt = clones.length;
// this loop ensures that the new itemId does not already exist
do {
matched = false;
num++;
newItmId = cfg.id + '-' + num;
for (i = j; i < cloneCt; i++) {
if (clones[i].itemId === newItmId) {
matched = true;
// if match is at the front, skip it next time...
if (i === j) {
j++;
};
// we found a match, no need to look anymore...
break;
}
}
} while (matched);// loop until the item[i].itemId does not match
return num;
},
cloneHandler: function (btn) {
// I want to call firstUniqueId() from here without going through MyView or MyOtherView
// (or if I do call through them, it would need to be generically, such that it could be from any view)
// because various other views use MyHelperMixin as a helper class
var cfg = btn.handlerConfig;
var xtype = cfg.prefix + '-' + cfg.id'
var itemToClone = btn.up(xtype);
var container = itemToClone.up();
var items = container.query(xtype);
// I want to call firstUniqueId() above (outside this method), NOT limitedUniqueId below...
var uniqueId = limitedUniqueId({
id: cfg.id,
clones: items
});
function limitedUniqueId(cfg) {
// same logic as firstUniqueId() above
return num;
}
},
});
Ext.define('MyView', {
extend : 'Ext.Container',
mixins: [
'myMixins/MyHelperMixin',
],
xtype: 'myview',
initComponent: function() {
var me = this;
me.items = [{ itemId: 'add-btn',
handlerConfig: {
prefix: 'someName',
id: 'myId',
},
handler: me.cloneHandler
}];
me.callParent(arguments);
},
});
Ext.define('MyOtherView', {
extend : 'Ext.Container',
mixins: [
'myMixins/MyHelperMixin',
],
xtype: 'myview',
initComponent: function() {
var me = this;
me.items = [{
itemId: 'clone-btn',
handlerConfig: {
prefix: 'anotherName',
id: 'anotherId',
},
handler: me.cloneHandler
}];
me.callParent(arguments);
},
});