PDA

View Full Version : how to invoke "loadData" of a controller from another controller



sekaijin
4 Dec 2011, 9:28 AM
Hi.

one thing I do not understand is how MVC with multiple controllers to communicate.

I have a controller that manages a tree containing node types.

when you click on a node of some type I would like to open a view of the controller of this type that displays the details of this node.

so I have a controller by data type.
node of the tree contains the id of the given display.

click on I found how to display the view. but I do not understand how to pass to load the record associated with the id.

I have not found a way in order to find the controller or the data store.

I did not find either how to apply to the load a record with his id.

I'll assume the controller of my tree I had to invoke the controller of that type, but I have not figured out how the controller referenced in question.

A+JYT
ps: sorry for my approximative english

sekaijin
5 Dec 2011, 1:08 AM
Hi

I do not know if it's the right way.

in my controller that manages the tree I call
this.application.getController ('xxx')
to get the controller for the type of node
it gives something like:

var view = Ext.getCmp(node.data.id + '_view');
if (undefined == view) {
view = Ext.createWidget('useredit', {
id : node.data.id + '_view',
title: node.data.text
});
Ext.getCmp('viewport').layout.regions.center.add(view);
}
Ext.getCmp('viewport').layout.regions.center.setActiveTab(view);
ctrl = this.application.getController('AM.controller.Users');
store = ctrl.getUsersStore();
store.load({
scope : this,
callback: function(records, operation, success) {
var r = store.getById(node.data.id);
view.down('form').loadRecord(r);
}
});

karpatyx@ya.ru
5 Dec 2011, 3:48 AM
Hi,

Nice way of communication between controllers is firing/listening application-level events.

E.g. I have controller 1 and controller 2. I want controller 2 to do something if something happened in controller 1.

Controller 1:


// something happened
this.application.fireEvent('needActionFromController2');

Controller 2:

init: function ()
{
this.application.on({
needActionFromController2: this.doAction,
scope: this
});
},

doAction: function()
{
// this code runs after controller 1 fires 'needActionFromController2' event
// do some controller 2 action here
}

P.S. Direct communication between controllers is bad practice. In ideal world they shouldn't know about each other and should communicate, for example, using application (or any other kind of) messages as described above. Exception is parent/child controllers (which, however, I'd avoid as well).

sekaijin
5 Dec 2011, 7:41 AM
Hi,
Thank you for this info.

it works much better as well.
tree controller on edit button

editSelection : function(tree, node) {
if (node.raw && ('application' == node.raw.type)) {
this.application.fireEvent('editApplication', {
id: node.data.id,
title: node.data.text,
method: 'edit',
iconCls: 'app'
});
}
},

app controller
init : function() {
this.application.on({
editApplication: this.edit,
scope: this
});
...
edit : function(cfg) {
var view = Ext.getCmp(cfg.id + '_view');
if (undefined == view) {
view = Ext.createWidget('appedit', cfg);
Ext.getCmp('viewport').layout.regions.center.add(view);
}
Ext.getCmp('viewport').layout.regions.center.setActiveTab(view);
var store = this.getAppsStore();
var r = store.getById(cfg.id);
view.down('form').loadRecord(r);
},

Bye