Quantcast
Channel: SAPUI5 Developer Center
Viewing all articles
Browse latest Browse all 789

OData model extension to consume plain REST services

$
0
0

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:

SAPGithubDemo.JPG

 

 

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");

 

Please remember: this is work in progress. Readonly access for now.
Feedback is very welcome.

Viewing all articles
Browse latest Browse all 789

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>