OData model extension to consume plain REST services
Hello to all,
While searching arround for ways to consume a plain REST server with OpenUI5, I noticed there were only a few possibilities.
The JSON model was one of them.
But also the extension found here :http://scn.sap.com/community/developer-center/front-end/blog/2015/09/29/extending-a-control-in-the-sapui5-library-sapuimodeljsonjsonmodel
However, they did not fit my needs.
So, I decided to write my own. With some success.
My new try can be found on Github.
https://github.com/LongDirtyAnimAlf/OpenUI5_RestModel
It includes an example to consume the Github REST api for its users and repos.
The result looks like this:
Main things to consider:
Normal REST does not prove metadata !!
Some of it is self-generated by this REST model.
Do do so, it needs info that is provided by setting a new parameter: the key.
(See source in Detail.controller.js)
oRepoTable.bindItems({
path : sRepoPath,
sorter : {
path : 'name',
descending: false
},
parameters :{
key:'id'
},
template:this._oTemplate
});
The new REST model self generates the metadata.
Many methods are are adopted to be able to create metadata.
RestModel.prototype._createmetakey = function(sPath, mParameters) {
var that = this;
if (!that.oMetadata.mEntityTypes[sPath]) {
that.oMetadata.mEntityTypes[sPath] = {};
}
var oEntityType = that.oMetadata.mEntityTypes[sPath];
if (!oEntityType["key"]) {
oEntityType["key"] = {};
}
var aKey = oEntityType["key"];
if (!aKey.name) {
if (mParameters && mParameters.key) {
aKey.name=mParameters.key;
} else {
if (that.sKey) {
aKey.name = this.sKey;
} else {
aKey.name = "ID";
}
}
}
jQuery.sap.assert(aKey.name, "Severe key error !!!");
};
// intercept creation of bindings to create metadata
RestModel.prototype.createBindingContext = function(sPath, oContext, mParameters, fnCallBack, bReload) {
// get key from params or settings
// mimics metadata info
this._createmetakey(sPath, mParameters);
return ODataModel.prototype.createBindingContext.apply(this, arguments);
};
Data retrieval is done by a normal ajax request.
This made it necessary to override some of the data retrieval methods.
Github uses different keys for different services.
users: login
repos: id.
To accomodate for this, keys are set in the main view and in the detail controller.
A main key can be set for the model as a whole.
var oModel = new sap.ui.model.rest.RestModel("https://api.github.com");
oModel.setKey("login");
Don Alfredo,
TBH I didn't get what are the limitations you are trying to avoid.
Could you please explain that?
Why can I use consume GitHub API using a simple JSONModel?
Thanks!
Sure you can consume REST (i.e. Github api) by using the JSON model !
But there it stops.
The OData model has the possibility of storing intermediate results of user edits, and advanced two way binding.
And the use of some server side intelligence.
And very automatic binding and efficient request for data.
I expect the OData model to gain many more usefull features in future.
And with the model that I wrote, these features are also available for normal REST.
HOWEVER
It would have been MUCH MUCH better, if the OData model would have been an extension of an advanced REST model.
I hope that one day, the people behind OpenUI5 will build an advanced REST model based on the current OData model, and with odata as an extension of this REST model.