momesana
15 Aug 2011, 8:13 AM
Hi,
I am firing an event from a model and wanted it to be handled by the controller that instantiated it. The controller instantiates the model and sets the event handler like this:
this.model = Ext.ModelManager.create({}, 'zf.model.DoorPlate');
this.model.on('dataChanged', function() {
console.log('data changed');
});
The function that fires the event looks as follows:
onLoad: function (response, options) {
// get result object
var obj = Ext.decode(response.responseText);
var result = obj.result;
// if result is empty return
if (result.id === undefined) {
return;
}
// update model
this.fields.each(function (field) {
this.set(field.name, result[field.mapping]);
}, this);
// fire event that things have changed
console.log('firing event');
this.fireEvent('dataChanged', this.data);
}
The entire code of both classes:
Model:
/**
* @classDescription Represents a DoorPlate.
*/
Ext.define('zf.model.DoorPlate', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int', mapping: 'ID', defaultValue: 0},
{name: 'room', type: 'string', mapping: 'STR_RAUM_NR', defaultValue: ''},
{name: 'roomId', type: 'int', mapping: 'OBJ_RAUM', defaultValue: 0},
{name: 'department', type: 'string', mapping: 'STR_ABTEILUNG', defaultValue: ''},
{name: 'departmentId', type: 'int', mapping: 'OBJ_ABTEILUNG', defaultValue: 0},
{name: 'layoutType', type: 'string', mapping: 'RADIO_LAYOUT', defaultValue: ''},
{name: 'field_0', type: 'string', mapping: 'STR_1_1', defaultValue: ''},
{name: 'fieldId_0', type: 'int', mapping: 'OBJ_1_1', defaultValue: 0},
{name: 'fieldType_0', type: 'string', mapping: 'RADIO_1_1', defaultValue: ''},
{name: 'field_1', type: 'string', mapping: 'STR_1_2', defaultValue: ''},
{name: 'fieldId_1', type: 'int', mapping: 'OBJ_1_2', defaultValue: 0},
{name: 'fieldType_1', type: 'string', mapping: 'RADIO_1_2', defaultValue: ''},
{name: 'field_2', type: 'string', mapping: 'STR_1_3', defaultValue: ''},
{name: 'fieldId_2', type: 'int', mapping: 'OBJ_1_3', defaultValue: 0},
{name: 'fieldType_2', type: 'string', mapping: 'RADIO_1_3', defaultValue: ''},
{name: 'field_3', type: 'string', mapping: 'STR_1_4', defaultValue: ''},
{name: 'fieldId_3', type: 'int', mapping: 'OBJ_1_4', defaultValue: 0},
{name: 'fieldType_3', type: 'string', mapping: 'RADIO_1_4', defaultValue: ''},
{name: 'field_4', type: 'string', mapping: 'STR_1_5', defaultValue: ''},
{name: 'fieldId_4', type: 'int', mapping: 'OBJ_1_5', defaultValue: 0},
{name: 'fieldType_4', type: 'string', mapping: 'RADIO_1_5', defaultValue: ''},
{name: 'field_5', type: 'string', mapping: 'STR_1_6', defaultValue: ''},
{name: 'fieldId_5', type: 'int', mapping: 'OBJ_1_6', defaultValue: 0},
{name: 'fieldType_5', type: 'string', mapping: 'RADIO_1_6', defaultValue: ''},
{name: 'field_6', type: 'string', mapping: 'STR_2_1', defaultValue: ''},
{name: 'fieldId_6', type: 'int', mapping: 'OBJ_2_1', defaultValue: 0},
{name: 'fieldType_6', type: 'string', mapping: 'RADIO_2_1', defaultValue: ''},
{name: 'field_7', type: 'string', mapping: 'STR_2_2', defaultValue: ''},
{name: 'fieldId_7', type: 'int', mapping: 'OBJ_2_2', defaultValue: 0},
{name: 'fieldType_7', type: 'string', mapping: 'RADIO_2_2', defaultValue: ''},
{name: 'field_8', type: 'string', mapping: 'STR_2_3', defaultValue: ''},
{name: 'fieldId_8', type: 'int', mapping: 'OBJ_2_3', defaultValue: 0},
{name: 'fieldType_8', type: 'string', mapping: 'RADIO_2_3', defaultValue: ''},
{name: 'field_9', type: 'string', mapping: 'STR_2_4', defaultValue: ''},
{name: 'fieldId_9', type: 'int', mapping: 'OBJ_2_4', defaultValue: 0},
{name: 'fieldType_9', type: 'string', mapping: 'RADIO_2_4', defaultValue: ''},
{name: 'field_10', type: 'string', mapping: 'STR_2_5', defaultValue: ''},
{name: 'fieldId_10', type: 'int', mapping: 'OBJ_2_5', defaultValue: 0},
{name: 'fieldType_10', type: 'string', mapping: 'RADIO_2_5', defaultValue: ''},
{name: 'field_11', type: 'string', mapping: 'STR_2_6', defaultValue: ''},
{name: 'fieldId_11', type: 'int', mapping: 'OBJ_2_6', defaultValue: 0},
{name: 'fieldType_11', type: 'string', mapping: 'RADIO_2_6', defaultValue: ''}
],
save: function () {
// create the object of parameters to be passed
var params = {};
this.fields.each(function (field) {
params[field.mapping] = this.get(field.name);
}, this);
Ext.Ajax.request({
url: '../fame/mf_nav.save_turschild',
scope: this,
noCache: false,
params : params,
success: function (response) {
var obj = Ext.decode(response.responseText);
console.dir(obj);
}
});
},
load: function () {
Ext.Ajax.request({
url: '../fame/wf_nav.gettuerschild',
noCache: false,
scope: this,
params: {
room_obj: this.get('roomId')
},
success: this.onLoad
});
},
onLoad: function (response, options) {
// get result object
var obj = Ext.decode(response.responseText);
var result = obj.result;
// if result is empty return
if (result.id === undefined) {
return;
}
// update model
this.fields.each(function (field) {
this.set(field.name, result[field.mapping]);
}, this);
// fire event that things have changed
console.log('firing event');
this.fireEvent('dataChanged', this.data);
}
});
Controller:
Ext.define('zf.controller.DoorPlate', {
extend: 'Ext.app.Controller',
views: ['DoorPlate'],
models: ['DoorPlate'],
init: function() {
this.model = Ext.ModelManager.create({}, 'zf.model.DoorPlate');
this.model.on('dataChanged', function() {
console.log('data changed');
});
this.control({
'doorplate': {
resize: this.onResize
}
});
},
onResize: function () {
var cmp = Ext.ComponentQuery.query('viewport > mainwindow doorplate')[0];
cmp.centerDoorPlate();
},
updateDoorPlate: function (config) {
},
saveDoorPlate: function () {
},
updateView: function (data) {
console.log('updateView() function to handle the dataChanged event');
console.dir(data);
}
});
Thanks in advance
I am firing an event from a model and wanted it to be handled by the controller that instantiated it. The controller instantiates the model and sets the event handler like this:
this.model = Ext.ModelManager.create({}, 'zf.model.DoorPlate');
this.model.on('dataChanged', function() {
console.log('data changed');
});
The function that fires the event looks as follows:
onLoad: function (response, options) {
// get result object
var obj = Ext.decode(response.responseText);
var result = obj.result;
// if result is empty return
if (result.id === undefined) {
return;
}
// update model
this.fields.each(function (field) {
this.set(field.name, result[field.mapping]);
}, this);
// fire event that things have changed
console.log('firing event');
this.fireEvent('dataChanged', this.data);
}
The entire code of both classes:
Model:
/**
* @classDescription Represents a DoorPlate.
*/
Ext.define('zf.model.DoorPlate', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int', mapping: 'ID', defaultValue: 0},
{name: 'room', type: 'string', mapping: 'STR_RAUM_NR', defaultValue: ''},
{name: 'roomId', type: 'int', mapping: 'OBJ_RAUM', defaultValue: 0},
{name: 'department', type: 'string', mapping: 'STR_ABTEILUNG', defaultValue: ''},
{name: 'departmentId', type: 'int', mapping: 'OBJ_ABTEILUNG', defaultValue: 0},
{name: 'layoutType', type: 'string', mapping: 'RADIO_LAYOUT', defaultValue: ''},
{name: 'field_0', type: 'string', mapping: 'STR_1_1', defaultValue: ''},
{name: 'fieldId_0', type: 'int', mapping: 'OBJ_1_1', defaultValue: 0},
{name: 'fieldType_0', type: 'string', mapping: 'RADIO_1_1', defaultValue: ''},
{name: 'field_1', type: 'string', mapping: 'STR_1_2', defaultValue: ''},
{name: 'fieldId_1', type: 'int', mapping: 'OBJ_1_2', defaultValue: 0},
{name: 'fieldType_1', type: 'string', mapping: 'RADIO_1_2', defaultValue: ''},
{name: 'field_2', type: 'string', mapping: 'STR_1_3', defaultValue: ''},
{name: 'fieldId_2', type: 'int', mapping: 'OBJ_1_3', defaultValue: 0},
{name: 'fieldType_2', type: 'string', mapping: 'RADIO_1_3', defaultValue: ''},
{name: 'field_3', type: 'string', mapping: 'STR_1_4', defaultValue: ''},
{name: 'fieldId_3', type: 'int', mapping: 'OBJ_1_4', defaultValue: 0},
{name: 'fieldType_3', type: 'string', mapping: 'RADIO_1_4', defaultValue: ''},
{name: 'field_4', type: 'string', mapping: 'STR_1_5', defaultValue: ''},
{name: 'fieldId_4', type: 'int', mapping: 'OBJ_1_5', defaultValue: 0},
{name: 'fieldType_4', type: 'string', mapping: 'RADIO_1_5', defaultValue: ''},
{name: 'field_5', type: 'string', mapping: 'STR_1_6', defaultValue: ''},
{name: 'fieldId_5', type: 'int', mapping: 'OBJ_1_6', defaultValue: 0},
{name: 'fieldType_5', type: 'string', mapping: 'RADIO_1_6', defaultValue: ''},
{name: 'field_6', type: 'string', mapping: 'STR_2_1', defaultValue: ''},
{name: 'fieldId_6', type: 'int', mapping: 'OBJ_2_1', defaultValue: 0},
{name: 'fieldType_6', type: 'string', mapping: 'RADIO_2_1', defaultValue: ''},
{name: 'field_7', type: 'string', mapping: 'STR_2_2', defaultValue: ''},
{name: 'fieldId_7', type: 'int', mapping: 'OBJ_2_2', defaultValue: 0},
{name: 'fieldType_7', type: 'string', mapping: 'RADIO_2_2', defaultValue: ''},
{name: 'field_8', type: 'string', mapping: 'STR_2_3', defaultValue: ''},
{name: 'fieldId_8', type: 'int', mapping: 'OBJ_2_3', defaultValue: 0},
{name: 'fieldType_8', type: 'string', mapping: 'RADIO_2_3', defaultValue: ''},
{name: 'field_9', type: 'string', mapping: 'STR_2_4', defaultValue: ''},
{name: 'fieldId_9', type: 'int', mapping: 'OBJ_2_4', defaultValue: 0},
{name: 'fieldType_9', type: 'string', mapping: 'RADIO_2_4', defaultValue: ''},
{name: 'field_10', type: 'string', mapping: 'STR_2_5', defaultValue: ''},
{name: 'fieldId_10', type: 'int', mapping: 'OBJ_2_5', defaultValue: 0},
{name: 'fieldType_10', type: 'string', mapping: 'RADIO_2_5', defaultValue: ''},
{name: 'field_11', type: 'string', mapping: 'STR_2_6', defaultValue: ''},
{name: 'fieldId_11', type: 'int', mapping: 'OBJ_2_6', defaultValue: 0},
{name: 'fieldType_11', type: 'string', mapping: 'RADIO_2_6', defaultValue: ''}
],
save: function () {
// create the object of parameters to be passed
var params = {};
this.fields.each(function (field) {
params[field.mapping] = this.get(field.name);
}, this);
Ext.Ajax.request({
url: '../fame/mf_nav.save_turschild',
scope: this,
noCache: false,
params : params,
success: function (response) {
var obj = Ext.decode(response.responseText);
console.dir(obj);
}
});
},
load: function () {
Ext.Ajax.request({
url: '../fame/wf_nav.gettuerschild',
noCache: false,
scope: this,
params: {
room_obj: this.get('roomId')
},
success: this.onLoad
});
},
onLoad: function (response, options) {
// get result object
var obj = Ext.decode(response.responseText);
var result = obj.result;
// if result is empty return
if (result.id === undefined) {
return;
}
// update model
this.fields.each(function (field) {
this.set(field.name, result[field.mapping]);
}, this);
// fire event that things have changed
console.log('firing event');
this.fireEvent('dataChanged', this.data);
}
});
Controller:
Ext.define('zf.controller.DoorPlate', {
extend: 'Ext.app.Controller',
views: ['DoorPlate'],
models: ['DoorPlate'],
init: function() {
this.model = Ext.ModelManager.create({}, 'zf.model.DoorPlate');
this.model.on('dataChanged', function() {
console.log('data changed');
});
this.control({
'doorplate': {
resize: this.onResize
}
});
},
onResize: function () {
var cmp = Ext.ComponentQuery.query('viewport > mainwindow doorplate')[0];
cmp.centerDoorPlate();
},
updateDoorPlate: function (config) {
},
saveDoorPlate: function () {
},
updateView: function (data) {
console.log('updateView() function to handle the dataChanged event');
console.dir(data);
}
});
Thanks in advance