cancel
Showing results for 
Search instead for 
Did you mean: 

JSONModel & writing to a RESTful backend

Former Member
0 Kudos

Hi,

Being a SAPUI5 beginner I have some conceptual questions about the sap.ui.model.json.JSONModel in combination with a RESTful backend: What is the best practice to implement writing operations (create, update, delete) to the backend?

I found countless examples how to read from the REST backend but still have to find a CRUD example with a JSONModel and a RESTful backend. My understanding is - an please correct me - that a OData model is able to automatically sync the model with the backend OData interface and that a JSON model cannot do this automatically with a RESTful service.

Think of a sap.ui.table.Table populated with data from a JSONModel which read from a REST backend. I guess I need to create e.g. a delete button and from its event handler delete the corresponding row in the table (which automatically deletes the record in the model thanks to two way binding) and in addition to that send a DELETE via Ajax to the backend. I guess, trying to send the delete back to the server first and then remove the row in the table and model would be better, allowing for error handling if there was an issue on the backend side. But this needs to be implemented for every create/update/delete operation in the whole application.

Wouldn't it be better to extend the JSONModel so it syncs the model to the backend server automatically? (Just for reference: Lindsay Stanger posted a nice blog post about extending the JSONModel with a bit of a different goal but I can imagine her post would be a starting point: ). I'm not sure how to properly handle errors from the backend side in this case. E.g. if a delete fails on the backend, also prevent the deletion in the table/model and present an error message.

I'm simply not sure whether I'm on the right track because this issue should affect lots of SAPUI5 users but I have not seen it solved, yet. So maybe I'm completely off...

Thank you and regards,

Erich

Accepted Solutions (0)

Answers (1)

Answers (1)

qmacro
Developer Advocate
Developer Advocate
0 Kudos

Hi Erich

You're on the right track. The challenge is, or rather, the difference between the OData Model and the JSON Model, is that OData is a protocol. REST is not a protocol. It is an architectural style, so there is no single way, or formalised way, of implementing CRUD. Of course, there are ways that are more "obvious" than others, but because of the lack of formality, there can be no single approach that could be implemented as standard in the JSON Model without others thinking that it would be "incorrect".

I'm not saying there couldn't be a coordinated effort by UI5 developers at large to standardise on a single way of doing things and collectively work towards a unified approach, but that hasn't happened. I would definitely encourage you to check out Lindsay's work (disclosure: Lindsay is a colleague and good friend of mine) as it could provide a good basis for what you're looking for. It also implements some further read logic (as opposed to write logic) that you might find useful.

Cheers

dj