• Products and Services
    • Products
    • Services
    • Solutions / Projects
    • Support
  • Download
  • Blog
  • Company
    • Team
    • For investors
    • Press
  • Community
  • Career
  • Contact
  • Products and Services
    • Products
    • Services
    • Solutions / Projects
    • Support
  • Download
  • Blog
  • Company
    • Team
    • For investors
    • Press
  • Community
  • Career
  • Contact

Blog

  • You are here:
  • Home
  • Calling OData actions and service operations with JayData
Blog categories
  • AngularJS
  • CorpJS
  • JavaScript
  • JayData
  • JayStack OData V4 Server
  • Microservices
  • molinio
  • MongoDB
  • Node.js
  • OData
  • ReactJS
  • TypeScript
Tag Cloud
.NET Core AngularJS asp.net core C# CorpJS dotnet Edm decorator EF EF Core Entity Framework Core examples extension GeoJSON Global query filters html5 hybrid app IndexedDb IndexedDb transaction indices indices sqlite javascipt data management JayData jaydata pro JaySvcUtil Kendo UI MEAN Stack membership levels Microsoft Dynamics CRM modules molin.io odata Queryable DB react RSS Ruby on Rails SAP soft-delete sql sqlite database transactions in SQlite tutorial TypeScript WebSQL

Calling OData actions and service operations with JayData

  • On 30th April 2013
  • Posted by admin
  • JayData

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.

1
2
3
<span class="kwrd"><</span><span class="html">script</span> <span class="attr">src</span><span class="kwrd">="/scripts/jquery-1.9.1.min.js"</span><span class="kwrd">></</span><span class="html">script</span><span class="kwrd">></span>
<script src=<span class="str">"/scripts/datajs-1.1.1beta.js"</span>></script>
<script src=<span class="str">"/scripts/jaydata.js"</span>><span class="kwrd"></</span><span class="html">script</span><span class="kwrd">></span>

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:

1
2
3
4
<span class="kwrd">var</span> context = <span class="kwrd">new</span> TodoDatabase.Service(<span class="str">'http://localhost:52999/todo'</span>);
       context.onReady(<span class="kwrd">function</span> () {
       <span class="rem">//Work with your data here</span>
});

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.

1
2
3
$data.initService(<span class="str">'http://localhost:52999/todo'</span>).then(<span class="kwrd">function</span> (context) {
    <span class="rem">//Work with your data here</span>
});

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

1
context.GetCompletedTodos().toArray(function(r) {...})

Calling service OData operations – processing the results in a promise

1
context.GetCompletedTodos().then(<span class="kwrd">function</span>(r) {...})

Calling service OData operations with parameters

1
context.GetCompletedTodos(param1, param2).toArray()

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

1
context.GetCompletedTodos(param1, param2, <span class="kwrd">function</span> () { }).then()

Calling service OData operations with named parameters

1
context.GetCompletedTodos({ p1Name: param1, p2Name: param2 }).toArray()

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

1
context.GetCompletedTodos({ p1Name: param1, p2Name: param2 }, <span class="kwrd">function</span> () { }).then()

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:

1
context.Todos.GetUnfinishedTodos()

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):

1
todoItem.Complete();

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

1
todoItem.GetRemainingHours().then(…)

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.

Source: JayData.org

0 Comments

Schema evolutions

Geography and geometry with JayData

Next thumb
Scroll
  • Home
    • Company
    • Career
    • Press
  • Products
    • JayData
    • molinio
    • Support
    • License
  • Community
    • Blog
    • CorpJS
Latest Blogposts
  • Getting familiar with Entity Framework Core 2.0: Global query filters

  • JayStack OData V4 Server – Type Definitions and Enums

  • Repatch – the simplified Redux

  • CorpJS Budapest #8

  • Creating Todo application from Molinio templates

Go to Blog!

  • Facebook
  • Linkedin
  • Rss
  • Twitter
  • Youtube
  • Github
  • Privacy Policy
  • Terms of Service
  • Give Us Feedback!
Copyright JayStack Technologies LLC. 2015. All Rights Reserverd. Non-JayStack logos/trademarks are copyright to their respective owners and do not constitute an endorsement of any JayStack products.

We are using cookies to give you the best experience on our website.

You can find out more about which cookies we are using or switch them off in settings.

JayStack
Powered by GDPR plugin

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.

3rd Party Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.

Please enable Strictly Necessary Cookies first so that we can save your preferences!