• .NET Developer Tools DevTools

    UI controls for ASP.NET AJAX, MVC, WPF,
    Silverlight, Windows 8 and Windows Phone

  • Hybrid Mobile Development Icenium

    Cross-platform Mobile Development Tool
    with cloud-based architecture

  • HTML5 / JavaScript Development Kendo UI

    Everything you need to build sites and
    mobile apps with JavaScript and HTML5

  • Testing Tools TestStudio

    One easy tool for Functional, Performance,
    Load and Mobile software testing

  • Web Presence Platform Sitefinity CMS

    Everything for your online business - content
    management, ecommerce, emarketing

  • Agile Project Management TeamPulse

    Simple and intuitive project management
    and collaboration software

Contact us

We are here for you.
  • usa+1‒888‒365‒2779
  • uk+44‒20‒7291‒0580
  • bg+359‒2‒8099850
  • de+49‒89‒2441642‒70
  • au+61‒2‒8090‒1465
  • emailsales@telerik.com
Your account Access to your products, updates and support
Telerik Product Families
  • Your Account
    Your Account
    Log in
  • ABOUT US

    About Telerik

    • Company
    • Press Center
    • Customers
    • Community
    • Careers
    • Contacts
Kendo UI - The way of HTML5
Products ▼
Kendo UI Web Kendo UI Mobile Kendo UI DataViz Server Side Wrappers
Demos Purchase Download
Blogs Documentation
Support ▼
Premium Forums StackOverflow Forums
Resources ▼

Featured Resource

Kendo UI Dojo


Blogs Code Library Demos Documentation FAQ Testing
Premium Forums Roadmap User Voice Videos Webinars More Resources
Contact Us Search
 

Blogs

Shields Up! Web Service abstraction with Kendo UI

Thursday, August 25, 2011 by Kendo UI Team Blog | Comments 5

When we last looked at the Kendo UI Data Source, we discovered that it could help simplify the process of talking to OData web service end points. By simply setting the "type" property to "odata," the Kendo UI data source automatically handles the nuances of talking to the Netflix OData API. By example:

var data = new kendo.data.DataSource({
    type: "odata",
    transport: {
        read: "http://odata.netflix.com/v2/Catalog/Genres"
    }
});

When the call to the OData endpoint executes, the Kendo UI Data Source automatically produces this RESTful URL:

http://odata.netflix.com/v2/Catalog/Genres?$format=json&$inlinecount=allpages&$callback=callback

How did the Kendo UI Data Source know what to do?

As it turns-out, the Kendo UI Data Source has an interesting extension point that makes it possible to "teach" the data source about any web service.

kendo.data.odata.js

If you look at the Kendo UI source, you discover (among many others) these two files: kendo.data.js and kendo.data.odata.js. The first file provides the majority of the Data Source implementation (as you would expect). The second file is interesting. It extends the kendo.data object to essentially define a default configuration for "odata" web services.

A snippet from this file reveals:

$.extend(true, kendo.data, {
        schemas: {
            odata: {
                type: "json",
                data: "d.results",
                total: "d.__count"
            }
        },
        transports: {
            odata: {
                read: {
                    cache: true, // to prevent jQuery from adding cache buster
                    dataType: "jsonp",
                    jsonpCallback: "callback", //required by OData
                    jsonp: false // to prevent jQuery from adding the jsonpCallback in the query string - we will add it ourselves
                },
...

As you can see, the file is properly configuring the known schema and transport settings for OData. By embedding this configuration information in this adapter, using the Data Source for OData requires much less manual configuration (see snippet #1).

Okay. Makes sense. Can we extend this model to do some interesting things?

Supporting OData CORS with a New Adapter

CORS is an emerging alternative for cross-origin async requests that replaces the need for JSONP hacks. As we demonstrated in a different blog post, when a server is configured with the CORS headers, you can easily use jQuery to query a web service on a different domain using Ajax.

The default Kendo UI OData adapter does not currently support CORS, though. It defaults to the more broadly supported JSONP configuration. Let's create a new Kendo UI Data Source adapter for OData with CORS support:

kendo.data.odataCors.js

(function($) {
    var kendo = window.kendo;

    $.extend(true, kendo.data, {
        schemas: {
            odataCors: {
                type: "json",
                data: "d.results",
                total: "d.__count"
            }
        },
        transports: {
            odataCors: {
                read: {
                    cache: true, // to prevent jQuery from adding cache buster
                    dataType: "json",
                },
                dialect: function(options) {
                    var result = ["$inlinecount=allpages"],
                        data = options || {};

                    //...detail omitted..

                    return result.join("&");
                }
            }
        }
    });
})(jQuery);

I've omitted some of the detail since it is generally the same as the original OData adapter. The most important things to note are the highlighted lines (6 and 13) where I set the new adapter name to "odataCors." This is the name I'll use when configuring the Kendo UI Data Source. Also notice that I've removed all JSONP configuration, clearly making this a CORS-specific OData fetch implementation.

To consume this new adapter in my Data Source and query a CORS-enabled OData service with Kendo UI, I simply change the "type" configuration:

var data = new kendo.data.DataSource({
    type: "odataCors",
    transport: {
        read: "http://odata.netflix.com/v2/Catalog/Genres"
    }
});

Pretty cool! Without changing anything other than the "type" name, I've altered how my data access is working. And really, I could have changed the original OData adapter, making the change completely transparent to my front-end code.

Let's try something a little different.

Abstracting the Twitter Search API

The Kendo UI Data Source works with much more than OData, of course. Let's use this same technique to build an adapter for the public Twitter Search API:

kendo.data.twitterSearch.js

(function ($) {
    var kendo = window.kendo;


    $.extend(true, kendo.data, {
        schemas: {
            twitterSearch: {
                type: "json",
                data: "results",
                total: "results_per_page"
            }
        },
        transports: {
            twitterSearch: {
                read: {
                    url: "http://search.twitter.com/search.json",
                    cache: true, // to prevent jQuery from adding cache buster
                    dataType: "jsonp",
                    jsonp: true
                },
                dialect: function (options) {
                    var result = ["callback=?"],
                        data = options || {};

                    if ("q" in data)
                        result.push("q=" + data.q);

                    if ("pageSize" in data)
                        result.push("count=" + data.pageSize)

                    return result.join("&");
                }
            }
        }
    });
})(jQuery);

As before, we've set our new adapter name ("twitterSearch") and configured the default behaviors for querying Twitter. Most interesting, we've set the Twitter Search URL endpoint and we've defined how a RESTful Twitter query should be constructed using "dialect" (mapping pageSize to the Twitter expected "count" parameter, and "q" for our query value).

Consuming this in the Kendo UI Data Source now only requires that we set a page size and define a query:

var ds = new kendo.data.DataSource({
    type: "twitterSearch",
    serverPaging: true,
    pageSize: 10,
    transport: {
        read: {
            data: { q: "Apple" }
        }
    }
});

You can check-out a live JSBin of this demo to see it in action.

Here's why this is cool:

  1. If the Twitter API search endpoint URL changes, I can simply update my adapter and all Kendo UI Data Sources using that adapter will be ready to go (imagine the benefit if you're working with an API that has a "sandbox" API endpoint and a production API endpoint)
  2. If the parameters for the Twitter API search change, that too is abstracted behind my adapter. A simple update to the adapter Dialect, and all Kendo UI data sources continue to work without any change.

Going Further

Now that you understand how these "adapters" work and why they are cool, you can start to imagine how this approach could be useful in your own development:

  • If you have an enterprise API, create an adapter that can be used to uniformly transition an app to point at DEV, TEST, and PROD endpoints
  • If you need to target multiple versions of an API that have different API parameter names, use an adapter to "hide" the differences from your front-end code.
  • Or, if you simply want to make it easier to work with a public API (like I have done with Twitter), create a simple adapter to keep your JavaScript DRY

There is also the potential for using this approach to do some high-level "mocking" of service data, where you have something like a "kendo.data.mockTwitterSearch.js" and "kendo.data.twitterSearch.js." When you reference the "mock" version of the adapter, it could be configured to return a fixed set of data instead of querying a remote service. Not as powerful or low-level as something like Mockjax, but another interesting application.

This is just one of the many cool features you can use in the Kendo UI Data Source. Enjoy the tip and stay tuned for more Kendo UI highlights.

About the Author
Todd Anglin is an avid HTML5, CSS3, and JavaScript advocate, and geek about all things web development. He is an active speaker and author, helping developers around the world learn and adopt HTML5. Todd works for Telerik as Chief Evangelist where his current technical focus in on Kendo UI. Todd is @toddanglin on Twitter.

5 Comments

  1. 1 Luc Morin 24 Mar 2012
    Todd,

    This blog post is the missing piece I was looking for to understand the "type" property of DataSource.

    Unfortunately, this doesn't seem to be documented anywhere. Could you see to it that this significant piece of information makes it into the official docs ?

    Thanks a lot.
  2. 2 Rama 24 Mar 2012
    Hi,

    I've tried with LightSwitch OData and it didn't work with kendo UI as Json format conversion required.

    Anyone found the solution, how to integrate LS OData services to Kendo UI.

    I've tried all ways 1) kendo.data.odataCors.js and it didn't worked.

    Please suggest the working sample for calling LS Odata.


    Regards
    Rama   
  3. 3 David 03 Mar 2013
    Sorry to add another branching topic but I am new to Kendo and your post is the only post anywhere that I see the d.__count value from js referred to. 

    Right now, when I try to load the grid, the function js file crashes on that value as null after my JSON is returned from the AJAX. 

    I tried adding a Total property with the collection count to the Model but it didn't help.  d.__count still crashes.

    Can you tell me what I am doing wrong?

    Thanks
  4. 4 dan 22 Dec 2012
    Can you post the js file source code for your data adapters?

    Thanks
  5. 5 dan 22 Dec 2012
    It doesn't seem to work with the latest kendoui 2012 Q3

    Thanks

Comment

  1. Click to add

  2. Click to add

  3. Click to add

  4.    
     
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
     
      
       
Blogs feed
Categories

  • Tutorials (26)
  • Release (33)
  • Browsers (7)
  • Extensions (3)
  • Tip of the Week (10)
  • Videos (5)
  • Concepts and Theory (13)
  • Misc. (25)
  • Framework Constructs (6)
  • Mobile (6)
  • UI Widgets (5)
  • Blogs (1)
Archive
  • 2013 May (7)
  • 2013 April (10)
  • 2013 March (9)
  • 2013 February (12)
  • 2013 January (10)
  • 2012 December (9)
  • 2012 November (11)
  • 2012 October (6)
  • 2012 September (7)
  • 2012 August (8)
  • 2012 July (10)
  • 2012 June (8)
  • 2012 May (10)
  • 2012 April (7)
  • 2012 March (13)
  • 2012 February (10)
  • 2012 January (6)
  • 2011 December (10)
  • 2011 November (4)
  • 2011 October (6)
  • 2011 September (5)
  • 2011 August (9)
Home Web Mobile DataViz Server Wrappers Whitepapers Surveys Chrome Icenium Contact Us

Kendo UI framework is developed by Telerik - a leading provider of UI components for web, desktop and mobile applications. Trusted by over 100,000 customers worldwide for our devotion to quality and industry-best technical support, Telerik helps professionals maximize their productivity and "deliver more than expected" every day.

kendoui - powered by html5, css3 & jquery
get social
  • Twitter
  • Facebook
  • Google plus
  • RSS
Privacy Policy | Branding Guidelines
Powered by Sitefinity CMS

Copyright © 2011 - 2013 Telerik Inc. All rights reserved.