PDA

View Full Version : How to add new record ( model) to the store.



rvsreddy68
31 Jul 2014, 6:53 AM
I have model which have child models as 'hasMany' relationship.
I load the data in grid and after editing or deleting from grid, I update the store records.
For new record I create a new model, set data to model and add record to store.


I add new record to store like this.
/////////////////////////////////////////////////////////////////
// adding record to store
var myParentModel= Ext.create('com.xx.myParentModel');
myParentModel.set("name", 'foo');
var mychildModel= Ext.create('com.xx.myChildModel');
mychildModel.set("chilldName", 'fooChild');
myParentModel.childModelObject().add(mychildModel);
myStore.add(myParentModel);
///////////////////////////////////////////////////////////////////////////////////////
I will update(newrecords,updatedrecords, deletedRecords) in one service call using ajax request.
see the code below.


///calling service
var myList= [].concat(myStore.getModifiedRecords(), myStore.getRemovedRecords( ));
var dataArray = [];</div><div>&nbsp;
for(x=0;x&lt;myList.length;x++){
dataArray.push(myList[x].raw);// for new record raw is undefined
}
Ext.Ajax.request({
url: myurl
method: 'POST
jsonData: &nbsp; Ext.JSON.encode(dataArray),
this.header || {'Content-Type': 'application/json;charset=utf-8'},
success: function() {
},
failure: function() {
}
});

My issue is, for newly added record(model) to the store, the model 'raw' property is always 'undefined'. How to set raw data for newly created record?
It works fine with edited record and deleted record.


?Early help is appriciated.

Thank you
Srini

mitchellsimoens
6 Aug 2014, 4:29 AM
Instead of using the set method after you create the model, pass the data when you create it:


var myParentModel= Ext.create('com.xx.myParentModel', {
name : 'foo'
});