PDA

View Full Version : Class Methods Not Defined



melnac
6 Apr 2014, 12:32 PM
Hi all,
i have this problem with class methods.

I have defined this class:



Ext.define("FrameworkLib.web.MenuStore", {
extend: 'Ext.data.TreeStore',

menuToolbar: Ext.create('Ext.toolbar.Toolbar', {}),

setChildren: function(item, menu) {
if (item.children) {
var menu1 = Ext.create('Ext.menu.Menu');
menu.add({ text: item.text, menu: menu1 });

Ext.each(item.children, function (e) {
setChildren(e, menu1);
});
}
else {
menu.add({
text: item.text
});
}
},

setMenu: function (item) {
var menu = Ext.create('Ext.menu.Menu');

Ext.each(item.children, function (obj) {
setChildren(obj, menu);
});

menuToolbar.add({
xtype: 'button',
text: item.text,
menu: menu
});
},

autoLoad: false,
proxy: Ext.create('Scripts.Library.MSAjaxProxy', {
url: 'DefaultViewController.aspx/GetMenuData',
autoLoad: false,
reader: {
type: 'json',
root: 'children',
getResponseData: function (response) {
var jsonData = Ext.JSON.decode(response.responseText);

var data = Ext.JSON.decode(jsonData['d']);

Ext.each(data.children, function (record) {
setMenu(record);
});

return this.readRecords(data);
}
}
})
});


Then i call the class FrameworkLib.web.MenuStore, in another class:


menuStore = Ext.create('FrameworkLib.web.MenuStore');
menuStore.load();


When the load() method is executed, i get an error saying:


TypeError: setMenu is not a function






But the functions are defined in the class, why a get this kind of error ?

Thanks in advance,
Giuseppe.

evandropoa
7 Apr 2014, 9:07 AM
Hello!!

There´s many things wrong, in your methods you dont use the scope (this) like in:




setChildren: function(item, menu) {
if (item.children) {
var menu1 = Ext.create('Ext.menu.Menu');
menu.add({ text: item.text, menu: menu1 });
Ext.each(item.children, function (e) {
this.setChildren(e, menu1);
}, this);
}
else {
menu.add({text: item.text});
}
}


You don't access children with item.children, but using item.childNodes (its a NodeInterface)

You dont access record data like this: item.text
but using: item.get('text') (or private item.data.text)

For last I think you shouldn't do it on store, but extend the menu component and listen to store.

Look your code working in (changed proxy of course): https://fiddle.sencha.com/#fiddle/4rq
(https://fiddle.sencha.com/#fiddle/4rq)

melnac
25 Apr 2014, 9:42 AM
Hi evandropoa,
you are right, the scope is the trick !

Thank you very much.

melnac