PDA

View Full Version : Is it possible for a controller to 'control' a model class



dr-dan
14 Oct 2012, 6:35 AM
The controller.control() function is a great and powerful thing. However it is only accepting a ComponentQuery.

Why is this not anything observable?

I have a Model with a custom create() function, which fires its own events depending on whether it was a successful or not. Pretty typical stuff, encapsulating only things the model should know about with the model, and being really discrete with events - which should be catchable by the controller.

At the moment we can use the 'on' method for an instance of a Model - but nothing for all instances of a Model e.g. the class.

Or am I mistaken?

vietits
14 Oct 2012, 11:54 PM
The controller.control() function is a great and powerful thing. However it is only accepting a ComponentQuery.

Why is this not anything observable?

All controllers in an Ext application use the same EventBus to catch events that are fired from components. EventBus does this by injecting Ext.Component.fireEvent() and therefore it's only able to catch events from components.



I have a Model with a custom create() function, which fires its own events depending on whether it was a successful or not. Pretty typical stuff, encapsulating only things the model should know about with the model, and being really discrete with events - which should be catchable by the controller.

At the moment we can use the 'on' method for an instance of a Model - but nothing for all instances of a Model e.g. the class.

Or am I mistaken?
Could you post your model code? How and where do you use this model?

dr-dan
14 Oct 2012, 11:59 PM
create: function (callback) {
var me = this;
var data = me.getData(false);
Ext.Ajax.request({
scope: me,
url: "/Profile/Create",
method: "POST",
jsonData: Ext.encode(data),
success: function (httpResponse, o) {
var jo = Ext.decode(httpResponse.responseText);
if (jo.errorType && jo.errorType.length > 0) me.fireEvent('errorOnCreate', jo.errorType);
else me.fireEvent('successOnCreate');
callback();
},
failure: function () {
alert(_localise('General.Server.Error'));
callback();
}
});
}


It sort of works now as I create an empty record in my controller and bind to the events above. Pass this into my form. On submit I read back the form with the original record and call `model.create()`. That for me is the essence of MVC. Theoretically, given the asynco nature of models I don't think it is unreasonable. I would prefer to do the binding at the class level as you can do with ComponentQuery(xtype).



var form = Ext.create('Pegfect.view.profile.CreateProfileStep1', {
itemId: 'createProfileStep1',
dataSets: me.getProfileDataSetsStore().getAt(0),
record: Ext.create('Pegfect.model.profile.CreateProfileStep1', {
listeners: {
errorOnCreate: me.onStep1Error,
successOnCreate: me.goStep2
}
})
});