PDA

View Full Version : How to work with non MCV classes



dedoz
16 Jan 2012, 9:06 PM
I have a class that is not a controller or a view, and i want to be present in all my code, is like a util class.
So i did this

main.js


Ext.create('Ext.app.Application', {
name: 'MUI',
appFolder: 'app',
controllers : ['SomeController1','OtherController2'],
autoCreateViewport: true,
launch: function()
{
MUI.currentUser = Ext.create('MUI.util.currentUser');
}
});


on a file in app/util/currentUser.js


Ext.define('MUI.util.currentUser',{
... some code will go here ...
});


So i create a instance of this currentUser class (its dymanic loaded ) and save the reference in my app namspace like MUI.currentUser.

Its this the correct way to do this ?
Also i have a problem, while it will be present for every controller method, i wont be available in the init method of those controllers i define in the aplication controllers : ['SomeController1'] since this init method is executed before my application launch method. Happens the same in any excuted code before the app launch, like while defining a view.

How can i have some objects that are present in my whole app ?

I dont want to set a property in the application controller because i can go into name colision doing something like


Ext.create('Ext.app.Application', {
name: 'MUI',
appFolder: 'app',
controllers : ['SomeController1','OtherController2'],
autoCreateViewport: true,
currentUser : Ext.create('MUI.util.currentUser'),
otherHelperObject1 : Ext.create('MUI.util.helper1'),
otherHelperObject2 : Ext.create('MUI.util.helper2'),
launch: function()
{

}
});

besides i dont know if ill always get a reference to the app, i know in the controller we can do something like this.application.currentUser, but while defining a view ? i wont get the this.application

Should i do this ?



Ext.onReady(function() {
MUI = {}
MUI.currentUser = Ext.create('MUI.util.currentUser');

Ext.create('Ext.app.Application', {
name: 'MUI',
appFolder: 'app',
controllers : ['SomeController1','OtherController2'],
autoCreateViewport: true,
launch: function()
{
}
});

});

this seems to work but im not sure its the best way

(sorry for my bad english)

mitchellsimoens
16 Jan 2012, 9:10 PM
I would use Ext.require and setup a path to it:


Ext.Loader.setPath('MUI', 'app');

Ext.require([
'MUI.util.currentUser'
]);

This will expect the MUI.util.currentUser to be in the app/util/currentUser.js file.

dedoz
16 Jan 2012, 9:41 PM
yeah i know that but ┬┐where to put the reference to the object created from that class? so that object is available on all my code, that includes
the init controller method
the views definition (Ext.define(MUI.view.SomeView)
etc

As a app config property ?
currentUser : Ext.create('MUI.util.currentUser'), and i explained in the initial post
This works for the controller init method (it will be avaible there) but not in the views definition.


Or under the MUI namespace ? like MUI.currentUser = Ext.create('MUI.util.currentUser');
and where to put this line ,
1) inside the app launch method doesnt work because i wont be avaible on controllers init or in view definition

2) Before Ext.create('Ext.app.Application'....
this is the best option i got, now is available on all my code (views, models, controllers, etc) but the problem is i dont get the facilities of dyamic loading so i have to declare each class/file im using on this util classes

so derives in this
main.js


Ext.Loader.setPath('MUI.util.currentUser', 'app/util/currentUser.js');
Ext.require(['MUI.util.currentUser']);

Ext.onReady(function() {
MUI = {}
MUI.currentUser = Ext.create('MUI.util.currentUser');

Ext.create('Ext.app.Application', {
name: 'MUI',
appFolder: 'app',
controllers : ['controller1','controller2'],
autoCreateViewport: true,
launch: function()
{ alert("getName from launch :"+ MUI.currentUser.getName);
// Works
}
});
});


in controller1 init method



Ext.define('MUI.controller.controller1', {
extend: 'Ext.app.Controller',
views : [view1],
init: function() {
alert("getName from controller init method :"+ MUI.currentUser.getName);
// works
}
});


in view1 definition


Ext.define('MUI.view.Viewport', {
extend: 'Ext.container.Viewport',
items: [{
html: 'My App Title - User : '+ MUI.currentUser.getName, // works
}]
});


the only problem is i dont get the dynamic loading magic in my util classes