Geeks With Blogs
Asif Maniar Software Engineer

At the recent Mix 2011 conference the datajs team talked about this new cross-browser JavaScript library that makes writing data centric web apps easier.

Its fairly simple to use and can be very powerful.

Here is a quick application I threw together using the library. The code uses datajs, jQuery, jQuery UI and jQuery templates.

Microsoft has an OData Service at which exposes all Mix sessions. I used this source to get a list of all sessions using the datajs library and bound them to a template using the templating library.

The below code uses the function to read the list of sessions and then binds the results to the sessionTemplate template.

//enable jsonp since this is a cross dom
OData.defaultHttpClient.enableJsonpCallback = true;
//read all sessions and bind to sessionsTemplate"$orderby=Title asc",
      function (data, request) {
       results = data.results;
                  getTags: getTags

The getTags method is passed into the template so the tags can be loaded for each session when it renders.

function getTags(sessionId) {
       //get list of all tags for this session"" + sessionId + ")/Tags",
        function (data, request) {
              //bind to the tagTemplate
              $("#tagTemplate").tmpl(data.results).appendTo("#" + sessionId);
     return "";

Here is the sessions template:

<script id="sessionTemplate" type="text/x-jquery-tmpl">
            <li><b><a href='${SessionCode}' target="_blank">${Title}</a></b> <p>${Abstract}</p>
                <p id='${SessionID}'>
                                              //call the getTags method to get the tags html
                    Tags: {{html $item.getTags($}}
The Tags template: (links to channel9 page for that tag)
<script id="tagTemplate" type="text/x-jquery-tmpl">
   <a href='${TagValue}' target="_blank">${TagValue}</a>&nbsp;
The Tags section in the sessionList template invokes the getTags function passed to it which in turn makes a request using the OData library for all tags for the current session.

Example - tags for session with ID of 150:

I also used the jQuery autocomplete plugin to create a autocompleted textbox so users can search for a session by title.

  source: function (request, response) {
      response($.map(results, function (item) {
       if (item.Title.toLowerCase().indexOf($.trim(request.term.toLowerCase())) != -1) {
              return {
                        label: item.Title,
                        sessionId: item.SessionID
        select: function (event, ui) {"" + ui.item.sessionId + ")",
        function (data, request) {

The above code uses the source method of the autocomplete plugin to populate the list of sessions and then overrides the select method to bind to the selected session.

You can read more about the autocomplete plugin here.

Note: This isnt the most efficient way of loading data as it doesnt use paging, background fetching or local storage provided by HTML5 and datajs. Just wanted to demo the odata portion of the datajs library and how it can be used.

Hope to change this to use local storage in the future.

You may view the entire sample here:







Posted on Monday, April 18, 2011 5:16 PM jQuery , JavaScript , mashup , odata | Back to top

Comments on this post: Mix 2011 Session List using OData, datajs and jQuery

comments powered by Disqus

Copyright © Asif Maniar | Powered by: