You are on page 1of 5

5/21/2015

ODataWriteSupport

OData Write Support


FordatabindingwiththeODatamodel,SAPUI5supportsseveralfeatures.

Note
Asthesefeaturesareexperimental,changesinfutureversionsofSAPUI5mayoccur.Currently,allwriteoperationshavetobeperformedbytheapplication
andaretriggeredsynchronously.
SAPUI5supportsthefollowingoperationsandfeaturesfortheODatamodel:
write
create
remove
update
read
getmetadataservicedocument
Crosssiterequestforgery(XSRF)tokensupportfortheRESTlibrary
refresh
Thedefaultbindingmodeissap.ui.model.BindingMode.OneWay.Youcanalsosetthebindingmodetosap.ui.model.BindingMode.TwoWay.

Create Request
ThecreatefunctiontriggersaPOSTrequesttoanODataservicewhichwasspecifiedduringcreationoftheODatamodel.
Theapplicationhastospecifythecollectionwheretocreatetheentryandtheentrydatapayload.
Togettheresultoftherequest,theapplicationcanhandovercallbackhandlerfunctions.Torecreateandupdatethebindings,arefreshistriggered
automaticallyafterthesuccessfulcreation.

https://sapui5.netweaver.ondemand.com/sdk/docs/guide/91f2a6ac6f4d1014b6dd926db0e91070.html

1/5

5/21/2015

ODataWriteSupport

Example
ThefollowingcodetriggersanewentryintheProductscollection:

varoEntry={}
oEntry.Name="IPad"
oEntry.Price="499$"
oModel.create('/Products',oEntry,null,function(){

alert("Createsuccessful")

},function(){

alert("Createfailed")})

Delete Request
TheremovefunctiontriggersaDELETErequesttoanODataservicewhichwasspecifiedduringcreationoftheODatamodel.Theapplicationhastospecify
thepathtotheentrywhichshouldbedeleted.Toupdatethebindingsinthemodel,arefreshistriggeredautomaticallyaftersuccessfuldeletion.
AsingleparameteroParameterscanbepassedintothefunctionandcancarryalloptionalattributes,suchasattributesforsuccessanderrorhandling
functionsaswellasETagattributes.ETagscanbeusedforconcurrencycontroliftheODataserviceisconfiguredtoprovidethem.Formoreinformation,see
thesectionaboutconcurrencycontrolandETagsintheODatadocumentation.
IfanETagisspecifiedinoParameters,itwillbeusedintheIfMatchHeaderinsteadofanyETagfoundinthemetadataofanentry.Ifnot,theETagis
retrievedfromtheentry'smetadata.IfnoETagisfound,noIfMatchHeaderwillbeused.

Example
ThefollowingcodedeletestheproductentrywithID=1fromtheProductscollectionofthedataservice:

oModel.remove('/Products(1)',null,function(){

alert("Deletesuccessful")

},function(){

alert("Deletefailed")})

https://sapui5.netweaver.ondemand.com/sdk/docs/guide/91f2a6ac6f4d1014b6dd926db0e91070.html

2/5

5/21/2015

ODataWriteSupport

Update Request
TheupdatefunctiontriggersaPUTrequesttoanODataservicewhichwasspecifiedduringcreationoftheODatamodel.SeetheODataModelAPIfor
informationaboutattributeuse.
Theapplicationhastospecifythepathtotheentrywhichshouldbeupdatedwiththespecifiedupdatedentry.
Afterasuccessfulrequesttoupdatethebindingsinthemodel,arefreshistriggeredautomatically.

Example
Thefollowingcodeupdatestheprice:

varoEntry={}
oEntry.Price="599$"
oModel.update('/Products(1)',oEntry,null,function(){

alert("Updatesuccessful")

},function(){

alert("Updatefailed")})

TheflagbMergehasbeenintroducedtoalsoallowaMERGErequesttoperformadifferentialupdate.

Read Request
ThereadfunctiontriggersaGETrequesttoaspecifiedpathwhichshouldberetrievedfromtheODataservicewhichwasspecifiedduringcreationofthe
ODatamodel.
Theretrieveddataisreturnedinthesuccesscallbackhandlerfunction.

https://sapui5.netweaver.ondemand.com/sdk/docs/guide/91f2a6ac6f4d1014b6dd926db0e91070.html

3/5

5/21/2015

ODataWriteSupport

Example
oModel.read('/Products(1)',null,null,true,function(oData,oResponse){

alert("Readsuccessful:"+JSON.stringify(oData))

},function(){

alert("Readfailed")})

Refresh
TheRefreshfunctiontriggersarefreshforeachbindingtocheckifavaluehasbeenupdated,ornot.Foralistbindinganewrequestistriggeredtoreloadthe
datafromtheserver.
Additionally,iftheXSRFtokenisnotvalidanymore,areadrequestistriggeredtofetchanewXSRFtokenfromtheserver.

XSRF Token
ToaddressthechallengeofCrossSiteRequestForgeryanODataservicemightrequireXSRFtokensforchangerequestsbytheclientapplicationforsecurity
reasons.Inthiscasetheclienthastofetchatokenfromtheserverandsenditwitheachchangerequesttotheserver.
TheODatamodelfetchestheXSRFtokenwhenreadingthemetadataandthenautomaticallysendsitineachwriterequestheader.Ifthetokenisnotvalidany
moreanewtokencanbefetchedbycallingtherefreshfunctionontheODatamodel.

Metadata
ThegetServiceMetadatafunctionreturnstheparsedmetadatadocumentasaJavaScriptobject.
Inthissection:
ExperimentalTwoWayBinding
Thetwowaybindingmodeenablestheapplicationtoupdatevaluesofasinglecollectionentrywithouttriggeringanimmediatechangerequest.create
anddeleteoperationsarenotcollectedandcanbecalledbytheapplicationasdescribedabove.

https://sapui5.netweaver.ondemand.com/sdk/docs/guide/91f2a6ac6f4d1014b6dd926db0e91070.html

4/5

5/21/2015

https://sapui5.netweaver.ondemand.com/sdk/docs/guide/91f2a6ac6f4d1014b6dd926db0e91070.html

ODataWriteSupport

5/5

You might also like