PDA

View Full Version : Problem with converted field



varunjyoti
31 Aug 2012, 2:20 AM
Model:



function converToMin(v,rec){
var periodicity = rec.get("periodicity"),
periodicityinMin = periodicity/(1000*60);
return periodicityinMin;
}


Ext.define('DG.model.Notification', {
extend: 'Ext.data.Model',
fields: [
{ name: "id"},
{ name: "description" },
{ name: "notificationtext" },
{ name: "periodicity" },
{ name: "periodicityinMin",convert:converToMin }
]
});


Store:


Ext.define('DG.store.Notifications', {
extend: 'Ext.data.Store',
model: 'DG.model.Notification',
proxy: {
type: 'ajax',
url: hostIP+'/dgcontroller/NotificationsServlet?json={"command":"getAllNotifications","id":0}',
reader: {
type: 'json',
root: 'result',
successProperty: 'success'
}
}
});


Grid:



Ext.define('DG.view.notifications.Grid', { extend: 'Ext.grid.Panel',
alias: 'widget.notifications_grid',
frame: true,
xtype: 'gridpanel',
store: 'Notifications',
columns: [
{
text: "ID",
flex:0.1,
dataIndex: 'id',
sortable: true
},
{
text: "Description",
flex: 0.5,
dataIndex: 'description',
sortable: true

},
{
text: "Periodicity (in minutes)",
flex: 0.2,
dataIndex: 'periodicityinMin',
sortable: true,
editor: {
xtype:'numberfield',
allowBlank: false,
minValue:1,
id:'notifications_periodicity',
blankText : 'Periodicity is required',
allowDecimals:false
}
},
],
plugins: [Ext.create('Ext.grid.plugin.RowEditing', {
clicksToMoveEditor: 1,
autoCancel: false

}) ],
viewConfig: {
forceFit: true
},

split: true,

initComponent: function () {


var rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
clicksToMoveEditor: 1,
autoCancel: false,
errorSummary: false
});

this.callParent();
}
});


The problem is with "Periodicity" column, which uses dataIndex as "periodicityinMin", which is a converted data field.

The probem is that when i edit "periodicity" column and hit "update" button of the editor, the update event does not get fired and nothing gets updated.
If i edit any other column along with periodicity, the update event fires but the value of periodicity does not get updated. It remains the same as it was before the edititng.

vietits
31 Aug 2012, 4:06 PM
With your model definition, "periodicityinMin" is a converted field. Its value is always calculated from "periodicity". You can not set its value directly. So each time you edit a "periodicity" cell which is linked to "periodicityinMin" field, it does not change the value of "periodicityinMin" field in respective record in store. This means this field will not be dirty and no need to update when store do sync with server.

varunjyoti
3 Sep 2012, 2:03 AM
With your model definition, "periodicityinMin" is a converted field. Its value is always calculated from "periodicity". You can not set its value directly. So each time you edit a "periodicity" cell which is linked to "periodicityinMin" field, it does not change the value of "periodicityinMin" field in respective record in store. This means this field will not be dirty and no need to update when store do sync with server.

But i want to send the value of periodicityinMin while updating record. I mean i have converted "periodcity" to "periodicityinMin". Now i want to use "periodicityinMin" for updating, adding the record

vietits
3 Sep 2012, 2:47 AM
What does your post data look like when updating record? Also check setting for config writeAllFields for your proxy writer to make sure it is set to true which is default.

varunjyoti
3 Sep 2012, 3:17 AM
What does your post data look like when updating record? Also check setting for config writeAllFields for your proxy writer to make sure it is set to true which is default.

I am not using a proxy writer to update the records. Instead, i send an ajax call manually with the data. When i update the record, i want to use "perodicityMin field to be sent to the server.

vietits
3 Sep 2012, 5:07 AM
It's better to post a test case.

varunjyoti
3 Sep 2012, 5:18 AM
I have posted the code for model, store and grid above. Now on the update event of the store, I am calling this function



updateNotification:function(store, record, operation, options){

var emailArr = record.data.emails,
periodicity = record.data.periodicityinMin;
json = '{"command":"updateNotification","id":' + record.data.id + ', "active":' + record.data.active + ', "description":"' + record.data.description + '","emails":"'+ record.data.emails+'","notificationtext":"'+record.data.notificationtext+'","periodicity":'+periodicity+'}';
Ext.Ajax.request({
url: hostIP+'/dgcontroller/NotificationsServlet',
params: {
json: json
},
success: function (response, request) {
var responseText = response.responseText;
//ToDo - Handle the Response from the server
var ResponseJSON = Ext.JSON.decode(response.responseText);
if (ResponseJSON.success) {
Ext.Msg.alert("Notifications",ResponseJSON.result);
}
else {
var message = ResponseJSON.error.errorText;
Ext.Msg.show({
title: "Notifications",
msg: message,
buttons: Ext.MessageBox.OK,
fn:function(){
DG.adminController.refreshNotification();
}

});
}
}

});
},


Please note that i want to use periodicityinMin field to send the data to the server to update the record. Let me know if anything else is needed.

vietits
3 Sep 2012, 5:52 PM
Is there any problem with value of 'periodicityinMin' field? I think your code is OK. However, I suggest you use record.get(fieldName) to get field value instead of directly access it as you did. Below is my suggestion:


updateNotification:function(store, record, operation, options){
// var emailArr = record.data.emails,
// periodicity = record.data.periodicityinMin;
//
// json = '{"command":"updateNotification","id":' + record.data.id + ', "active":' + record.data.active + ', "description":"' + record.data.description + '","emails":"'+ record.data.emails+'","notificationtext":"'+record.data.notificationtext+'","periodicity":'+periodicity+'}';
Ext.Ajax.request({
url: hostIP+'/dgcontroller/NotificationsServlet',
params: {
json: Ext.encode({
command: 'updateNotification',
id: record.getId(),
active: record.get('active'),
description: record.get('notificationtext'),
emails: record.get('emails'),
periodicity: record.get('periodicityinMin')
})
},
success: function (response, request) {
var responseText = response.responseText;
//ToDo - Handle the Response from the server
var ResponseJSON = Ext.JSON.decode(response.responseText);
if (ResponseJSON.success) {
Ext.Msg.alert("Notifications",ResponseJSON.result);
}
else {
var message = ResponseJSON.error.errorText;
Ext.Msg.show({
title: "Notifications",
msg: message,
buttons: Ext.MessageBox.OK,
fn:function(){
DG.adminController.refreshNotification();
}
});
}
}
});
},

varunjyoti
3 Sep 2012, 9:07 PM
Ok thanks for the the suggestion. But the above function i.e updateNotification function, does not get called if i just edit the periodicityinMin field i.e update event does not get fired. However if i edit any other field, update event gets fired. but still the edited value of periodicityinMin is not available in the above piece of code.

vietits
4 Sep 2012, 12:34 AM
As I said in one of my posts, because 'periodicityinMin' field is a converted field and its value is calculated from 'periodicity' field. So, its value will not be changed unless you change value of 'periodicity' field. Though you edit this field on the grid but its value won't be change this way so the 'update' event on store won't be fired if you modified only this field.