PDA

View Full Version : How to post a new record with REST



cassiopeia
15 Oct 2012, 6:15 AM
Hi,
I need to create new records in the SAP database.
Reading is no problem, but I don't know which code i need to perform a POST operation.

I work with MVC.
My store is currently :


Ext.define('LeaveRequest.store.MyRequestsStore', {

extend : 'Ext.data.Store',

requires : ['LeaveRequest.proxy.oData', 'Ext.data.reader.Xml',
'Ext.data.writer.Xml'],

config : {
storeId : 'myrequestsstore',
model : 'LeaveRequest.model.Request',

proxy : {

headers : 'X-CSRF-Token : ... ; '
+ 'Content-Type : application/atom+xml ; '
+ 'X-Requested-With : XMLHttpRequest',

type : 'odata',

enablePagingParams : true,
withCredentials : true,
username : LeaveRequest.util.Config.getUsername(),
password : LeaveRequest.util.Config.getPassword(),

url : ' ... ',
reader : {
type : 'xml',
record : 'properties'
}
}
},
loadAll : function() {

var proxy = this.getProxy();
proxy.setExtraParams('');
this.load();

},

createEntry : function ( ... ) { ... }

});

I would like to make a new function 'createEntry' which receives the necessary parameters.
But what do I have to write in this method?

Thank you

crysfel
15 Oct 2012, 6:39 AM
You need to add a writer to your store, this way every time you add/update/remove a record, the store will synchronize your store with your server.

Regards

cassiopeia
15 Oct 2012, 6:46 AM
So, in the proxy:



writer:
{
type:'xml',
method:'POST'
}


Does it need something else?
And how do I specify in my createEntry method that I want to post the model?


createEntry : function(employeeID, absenceTypeCode, startDate,
endDate) {

model = Ext.create('LeaveRequest.model.Request', {

'EmployeeID' : employeeID,
'AbsenceTypeCode' : absenceTypeCode,
'StartDate' : startDate,
'EndDate' : endDate
});

}

Thank you for your reply =)

fschaeffer
16 Oct 2012, 9:25 PM
If you're defining your proxy on the model (and not on the store) you could simply do a model.save() which would POST your model to the defined URL ...

Luc
8 Nov 2012, 2:30 PM
Hi,

I assume you are using the SAP OData connector (oData.js file). It already defines appropriate reader and writer configs, so you don't need to configure them again. Like the comment from fshaeffer says, add the proxy to the LeaveRequest.model.Request instead of the store and you can then model.save()

Hope this helps.

cassiopeia
9 Nov 2012, 12:25 AM
every documentation i read on sencha, says that it's best to put the proxy inside the store.
i will try it on the model and it maybe helps for my problem on
http://www.sencha.com/forum/showthread.php?248192-SAP-bug-create&p=909576#post909576
which you also responded.
thank you for your help, i hope it works =)

Luc
9 Nov 2012, 3:38 AM
Hi,

Yes, you can have proxies on either the store or the model. I prefer them on the models, for reasons described in:
ST Documentation: proxy guide (http://docs.sencha.com/touch/2-0/#!/guide/proxies)

Also the "SAP ODAta connector readme.pdf" recommends proxies on the models:

A Sencha Touch proxy connects models and stores with servers that house the
data used by your app. Proxies are configured with parameters to complete that
connection, such as the server URL, data format (for example XML or JSON),
and authentication needed to make requests on the server. (Note: the current
implementation of the SAP oData Connector supports only XML.) Proxies can be
defined on either stores or models, but the recommended place is the model.

When testing connectivity to backend servers, I always start out with simple model read, create, update, delete operations and then move on to stores.