As a follow up to Sandeep’s introduction to our new Mobile Design Templates, I thought I would specifically visit a different template: the Calendar View.  I also wanted a chance to review some of the code assembled for the Hybrid Mobile Webinar and retest it with the latest material.  The end result?  A super simple Calendar app that hooks into Salesforce events via the REST API.  No fear, though, I’ll be updating and evolving this app over a series of blog posts.

Introducing SObjectData.js

One thing we wanted with our recent additions and updates to the Mobile Packs was to be able to support the new SmartSync framework.  While the original examples for SmartSync included Backbone.js as a framework, we also wanted to be able to use SmartSync with lower level libraries like jQuery Mobile.  The Mobile Design Templates also require some jQuery to work their magic.  For the jQuery Mobile Pack, and libraries like the Mobile Design Templates, I wrote a new library to abstract SmartSync without requiring an MVC style library like Backbone or Angular: SObjectData.  This library runs on top of ForceTK (or RemoteTK) and SmartSync to provide easy data access and handling.

This application will use a very small portion of SObjectData – just fetching data and accessing it.  I’ll cover it in more detail in upcoming posts, but for now you can preview the Readme.md for a full description of the library’s functionality.

Pre-requisites

This application will be designed for a local hybrid application, designed to work with our Mobile SDK handle authentication.  So with that, and the Mobile Design Template, and SObjectData … we will use:

  • Mobile SDK: For the Salesforce OAuth plugin as well as the SmartSync framework.
  • Mobile Design Templates: For our user interface.  This will also bring in jQuery and Underscore.
  • SObjectData: Data access and manipulation against SmartSync.

Authentication

Since this is a local hybrid app – we’ll leverage the Mobile SDK for authentication.  This will require setting up a Connected App, and setting the keys correctly using either forcedroid or forceios to generate a sample application.  From there, you can pretty much keep bootconfig.json from the www directory and replace the rest with our sample from github.  If this is new to you, check out the Mobile SDK documentation to get up and running.

Querying Data

Accessing your data with SObjectData is done via the fetch method of an SObjectData instance.  Just give it three parameters: type (SOSL or SOQL), query and callback.  For our calendar app, we’ll query based on a time span (either TODAY or THIS_WEEK) and then divide all the events into different arrays we can association with our template.  First we instantiate our SObjectData.  This object will be associated with a specific set of data (in this case our events), knows the SObjectType and fields associated with that data set and makes it easy to access and manipulate the data:

var Events = new SObjectData();

Now in this instance, we don’t need to define either the SObjectType or fields – SObjectData will infer all of that based on the result of our query:

function refreshData(span) {
 Events.fetch('soql','Select Id, Subject, StartDateTime, ActivityDate, ActivityDateTime, DurationInMinutes, Description, Location from Event WHERE ActivityDate = '+span+' ORDER BY StartDateTime ASC',divideEvents);
 }

Using the Calendar View Template

The Calendar View has two main sections – the first sets up the dates on the left side and a visual indicator as to the number of events (and/or tasks) associated with the date, and the right side which will show the full details for a specific day.  The application has taken the records from our query and divided them into arrays for each day of the week, and then associated all of those into one array (along with a couple of other convenient arrays like days of the week).  We can now bind these to an underscore template:

<div class="content">
 <div class="span-50 padding-right-gutter-half">
   <h1 class="padding-bottom-gutter"></h1>
   <ul id="week-planner" class="week-planner">
   <script id='events-template' type='text/template'>
   <% for(var i = 0; i < 7; i++){
   %>
   <%
   if(Week_Dates[i] != null) {
   %>
     <li data-date="<%=Week_Dates[i]%>">
     <div class="date"><B><%=Days_of_Week[i]%></B><BR /><%=Week_Dates[i]%></div>
     <ul class="week-planner-items">
      <% for(var x = 0; x < Week_Events[i].length; x++) {%>
       <li class="event"></li>
      <%}%>
     </ul>
     </li> 
   <%
    }
    } %>
  </script>
  </ul>

 <!-- .week-planner -->
 </div><!-- .span-50 -->
 <div id="days-events" class="span-50 padding-left-gutter-half">
 <script id='days-template' type='text/template'>
   <% 
   for(var i = 0; i < 7; i++){
   %>
   <%
   if(Week_Dates[i] != null) {
   %>
     <div class="date-content" id="date-content-<%=Week_Dates[i]%>">
     <h1 class="event">Events</h1>
     <div class="events">
     <% for(var x = 0; x < Week_Events[i].length; x++) {%>
      <h3><%=Week_Events[i][x].Subject%></h3>
      <P><%=activityTimeSpan(Week_Events[i][x])%></P>
      <P><%=Week_Events[i][x].Location%></P>
      <P><%=Week_Events[i][x].Description%></P>
     <%}%>
  </div>
 </div>
 <%
 }
 } %>
 </script>

 </div><!--.span-50-->
 </div><!-- .content -->

With a little jQuery:

 $('#week-planner').empty();
 $('#days-events').empty();

 $('#week-planner').append(eventsTmpl({
  Week_Dates : Week_Dates,
  Week_Events : Week_Events
 }));
 $('#days-events').append(daysTmpl({
  Week_Dates : Week_Dates,
  Week_Events : Week_Events
 }));
 calendarObj.init();

Note the calendarObj.init call.  This is an alternative to the onTemplateReady event that Sandeep used in his tutorial if we want to refresh only a specific view out of the the Mobile Design Templates

Adding the Tabbed View Template

For a little variety, the application can switch between events for Today and events for This Week.  For simplicity, we’ll call back to the API’s for each button press – but in a future iteration we’ll make better use of a local cache and refresh mechanism.  To make our application a tabbed one, we just add the HTML from the template:

<div id="tabbed-list-view-nav" class="tabbed-list-view-nav">
 <a id="today-tab" class="span-50 on">Today</a>
 <a id="week-tab" class="span-50">Week</a>
 <div id="tabbed-list-view-nav-arrow" class="tabbed-list-view-nav-arrow">&nbsp;</div>
</div>

And then bind some events with jQuery:

$('#today-tab').click(function() {
   refreshData('TODAY');
 });

 $('#week-tab').click(function() {
   refreshData('THIS_WEEK');
 });

ActivityDateTime to Date

Those of us who have dabbled with Apex Date formats and JavaScript in the past have learned that the two don’t always get along.  As part of this app, I’ve got a simple function to take an event record, pull the ActivityDateTime and then convert it to a proper JavaScript date in UTC (to keep time zone shifts correct):

function activityToDateTime(record){
   activity_string = record.ActivityDateTime;
   activity_array = activity_string.split("T");
   date_array = activity_array[0].split("-");
   time_array = activity_array[1].split(".")[0].split(":");
   activity_month = parseInt(date_array[1])-1;
   return new Date(Date.UTC(date_array[0],activity_month,date_array[2],time_array[0],time_array[1]));
 }

Quo vadis?

As advertised – simple calendar application.  You can see how libraries like SObjectData, the Mobile Design Templates and our Mobile SDK make it easy to get the basics up and running.  I’ll continue to use this application to demo what SObjectData can do – in particular full CRUD functionality as well as hooking into the SmartSync framework to provide proper cache functionality.  I’ll keep it updated as part of the SObjectDataJS repo – so feel free to fork the project off of github if you’d like to branch off and tinker with it.  As always, send me feedback in the boxes below or catch me on Twitter @joshbirk.

tagged , , , Bookmark the permalink. Trackbacks are closed, but you can post a comment.
  • mohit srivastav

    You guys are abstracting and abstracting the hbrid mobile application development.I wont be surprised one day it would be just point and click to get enterprise app.One concern i have is how to handle huge data of enterprise in mobile apps ?