Calling OData actions and service operations with JayData

Author: Robert Bonay April 30th, 2013


Most of the OData examples and applications operate with EntitySets, which is a very flexible way for data collaboration if we want the client applications to implement the business logic. OData service operations, entity-level and entityset-level actions provide us the opportunity to centralize some of our business logic and encapsulate the code on the server-side. The OData standard defines the way you can can consume these services and JayData library helps you to access these remote functions from JavaScript in a comfortable way.

Setting up the OData context for JayData

A context object must be initialized in order to initialize the connection with the remote endpoint and generate the client-side “proxy classes” for data access; JayData provides a static and a dynamic alternative to do so, both of them are based on the $metadata of the service.

Including the necessary libraries

JayData has no external dependency, but the OData provider is using the datajs library and we want to use promises in our code, which we will achieve by leveraging jQuery promises.

Static context initialization

Static context initialization requires to run the JaySvcUtil.exe, which generates a JavaScript file with the necessary data model and metadata. Including this file make you able to initialize the context with the following snippet:

Dynamic context initialization

The dynamic context creation comes handy if you want to avoid re-generating the entity definitions from time-to-time and you know the schema of you published service.
With $data.initService() you can be sure that you are using the up-to-date model as it is generated from the service metadata. But this requires the $metadata document to be accessible from the origin of your app.

This is fine, we can CRUD our entitysets and call our service operations and actions now. All the code in the next sections must be copied to replace the comments in the snippets above.

Accessing OData service operations

Service operations are defined on the server at the service level, like http://host/YourService/GetCompletedTodos

Calling service OData operations – processing the result in a success callback

Calling service OData operations – processing the results in a promise

Calling service OData operations with parameters

Calling service OData operations with parameters – optional success callback and promise

Calling service OData operations with named parameters

Calling service OData operations with named parameters – optional success callback or promise

Accessing OData entityset-level actions

EntitySet-level actions are useful if your don’t have the reference to a particular entity or you want to perform your operation on multiple entities.

The OData server publishes these actions at a similar URL: http://host/YourService/Todos/GetUnfinishedTodos

JayData helps you to call this service at the other end of the wire and processing the results using the following JavasScript instruction:

Accessing entity-level actions

On the analogy of todo example, the entity-level Complete action is published at the following URL: http://host/YourService/Todos(13)/Complete

The matching JayData call (assuming you already have the todoItem with ID = 13):

Passing parameters, success callbacks to actions and using promises can be achieved just like we have seen in the service operation section.

All the features mentioned in this document require JayData v1.3, download the latest JayData version now and learn more about the OData standard!

Publishing operations and actions on the server-side

You might ask the way of publishing OData service operations and actions, but at this moment I cannot tell you a server-side technology, which implements both… nevertheless, JayData library is ready to consume the upcoming releases of WCF Data Services and WebAPI OData. MS guys are working hard and releasing new versions regularly, we will update this thread with the outcomes. Until then, here are the current options: Service operations can be published with WCF Data Services (.NET), JayData server (node.js) or JayStorm Platform as a Service (cloud), actions can be implemented using WebAPI OData.