As of RC1 Refresh 1 you can use service operations (server side functions) with JayData. To create one and then consume it with JavaScript is just a matter of minutes and a couple of line of codes – with the right tools in hand.
OData Service Operations provide a powerful way of exposing data with maximum control over the data given out.
As usual the JayDataNorthwind sample project will be used as a base for the examples below.
Create Service Operation with WCF Data Service
Our sample will limit the number of the results to a maximum 5 and also enforces that some price filtering is applied.
1 2 3 4 5 6 7 8 9 10 |
public class Northwind : DataService<NorthwindEntities> { // This method is called only once to initialize service-wide policies. public static void InitializeService(DataServiceConfiguration config) { config.SetEntitySetAccessRule("*", EntitySetRights.All); config.SetServiceOperationAccessRule("*", ServiceOperationRights.All); config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2; } [WebGet] public IQueryable<Product> GetBudgetProducts(int budgetLevel) { return this.CurrentDataSource .Products .Take(5) .Where(p => p.Unit_Price < budgetLevel * 100); } } |
Generate Northwind.js
Using the JaySvcUtil.exe command line tool generate Northwind.js from the metadata exposed by the WCF Data Service.
1 |
c:>NorthwindWeb\JaySvcUtil.exe --metadataUri http://{yourSvcAddress}/Northwind.svc/$metadata --out Scripts\Northwind.js |
Call GetBudgetProducts from JavaScript
Our newly forged server method appears in the intellisense list with parameter hint
Service Operations behave like EntitySets!
Service calls due to their asynchronic nature does not return their value. Instead they accept a handler method or return a promise to an array of values.
Handler method way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<head> <title></title> <script type="text/javascript" src="Scripts/jquery-1.6.4.js"></script> <script type="text/javascript" src="Scripts/JayData-rc1-r1.js"></script> <script type="text/javascript" src="Scripts/Northwind.js"></script> <script> northwind = new Northwind .NorthwindEntities({ name: 'oData', oDataServiceHost: 'http://localhost:39323/Northwind.svc' }); $(function () { northwind.GetBudgetProducts(5).toArray( function (products) { for(var i = 0, l = products.length; i < l; i++) { $('#output').append(products[i].Product_Name + "<br />"); }; }); }); </script> </head> <body> <div id="output"> |
Results:
1 2 3 4 5 |
Chai Chang Aniseed Syrup Chef Anton's Cajun Seasoning Chef Anton's Gumbo Mix |
This is what goes to the server in the form of a http request
If you don’t like the handler methods way or need to handle multiple async requests in a simple way then you can treat the toArray() result as a so called promise. JayData does not have a Promise implementation rather it defines a promise interface. A default promiseimplementation with the jQuery.deferred library is included in the JayData library and is set as default.