JayData.org

How to build a Simple OData-based AJAX application with Visual Studio 2010

Author: Peter Aron Zentai May 21st, 2012

, , , , , ,


By completing this tutorial you will be able to consume an OData service from JavaScript with the elegance and simplicity of the JavaScript Language Query, without ever writing down an $.ajax statement or manually building OData query strings or payloads.

Notes:

  • This howto is built with the Northwind sample database, wrapped in an EntityFramework 4.0 EDMX and published with a WCF Data Service. You can grab this example project from CodePlex.
  • This walkthrough intentionally lacks the use of any kind of view or template engines to keep the material focused on its main topic: accessing and manipulating data with JayData.

Why use JayData?

With JayData you can work with online data in JavaScript on a way that was previously only possible for .NET and Silverlight developers. Instead of approaching the OData (or other storage) endpoint from a protocol level with handcrafted query strings and HTTP requests fired off from the middle of an event handler, JayData provides a typed abstraction layer: the EntityContext with typed EntitySets and Entities, and the native syntax of JavaScript for querying and modifying data.

 

Before we begin, see it in action below:

One possible way

The JayData way
JavaScript Language Query (JSLQ)

 

Get ALL Categories!

 

Get ONLY the product names from the list of products that are either Beverages (Category_Id = 1) or contain tofu in the product name regardless of the case,

sorted by the product name.

A typed OData context provides intellisense on the Entity and EntitySet level.

You can generate such a context with the JaySvcUtil.exe, we will talk this about later.

image_thumb[17]

CategoryId? CategoryID? or Category_ID?

Intellisense list of entity fields comes in handy in a number of times.

image_thumb[16]

The fluent API provides for simple, readable code

…that is easy to maintain even in the long run

image

How to setup the Northwind OData demo environemnt?

The Northwind OData demo environment can be downloaded from here or from this link at CodePlex.

1) Open Visual Studio 2010 and create a new Empty Web Application, name it NorthwindDemo.

image

2) Add ASP.NET folder App_Data

3) Drag the Northwind.sdf sample database from

C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v4.0\Samples into the App_Data folder.

image

4) Add a new ADO.NET Entity Data Model and name it Northwind

image

 5) Choose Generate from database

image

6) Accept the default selection of the data connection

If you can not see Northwind.sdf like in the picture then make sure you have SQL Server CE 4.0 support installed. Also you might need to open the Northwind.sdf database with a double click to have it appear in the list.image

7) Include all the tables and press Finish to get an EDMX (that is not so nicely arranged then mine)

image

 

image

8) Add a new WCF Data Service and name it Northwind.svc

image

9) Open the Northwind.svc file and edit its contents following the comments in the code:

10) Right click on the Northwind.svc and select “View in Browser”. Congratulation, you have and operational OData service.

image

Getting JayData and preparing our HTML5 application

1) Open Tools > Library Package Manager > Package Manager Console, and type

2) Install-Package JayData

 

image

3) The  package contains the JayData JavaScript library (and its dependencies) and the JaySvcUtil.exe command line tool.

 

image

4) Use the JaySvcUtil to generate Northwind.js from the service metadata uri: /Northwind.svc/$metadata

The syntax for JaysvcUtil is blogged about here.

image

Northwind.js is generated in you web project folder. You may have to switch “show all files” in the solution tree to be able to access Northwind.js from Visual Studio.

For those with deep passion in their heart toward JavaScript, here is what gets generated. Others may freely skip.

5) Include Northwind.js in your project and place into the Scripts folder

6) Add an empty html page and name it index.html

Make sure that the doctype is HTML5 that is: <!DOCTYPE html>

7) Place JayData.js and its dependencies in the index.html file

image

UPDATE:

As of JayData 1.1 and above you need to also include jaydataproviders/odataprovider.js in your page for the following examples to work without modification.

How to create a simple product creator application?

List categories

List products for a category

Extended the script block with the following lines. Notice the match in the jQuery delegate for the category class (class=category).

Create a new product from code

To invoke this function place a button somewhere in the html.

and that’s all folks…


, , , , , ,