Create a Mobile Sync Model and a Collection

Now that we’ve configured the HTML infrastructure, let’s get started using Mobile Sync by extending two of its primary objects:
  • Force.SObject
  • Force.SObjectCollection
These objects extend Backbone.Model, so they support the Backbone.Model.extend() function. To extend an object using this function, pass it a JavaScript object containing your custom properties and functions.
  1. In the <body> tag, create a <script> object.
  2. In the <script> tag, create a model object for the Salesforce user sObject. Extend Force.SObject, and specify the sObject type and the fields we are targeting.
    1app.models.User = Force.SObject.extend({
    2    sobjectType: "User",
    3    fieldlist: ["Id", "FirstName", "LastName", 
    4        "SmallPhotoUrl", "Title", "Email", 
    5        "MobilePhone","City"]
    6})
  3. Immediately after setting the User object, create a UserCollection object to hold user search results. Extend Force.SObjectCollection, and specify your new model (app.models.User) as the model for items in the collection.
    1app.models.UserCollection = Force.SObjectCollection.extend({
    2    model: app.models.User,
    3    fieldlist: ["Id", "FirstName", "LastName", 
    4        "SmallPhotoUrl", "Title"],
    5
    6});
  4. In this collection, implement a function named setCriteria that takes a search key and builds a SOQL query using it. You also need a getter to return the key at a later point.
    1<script>
    2    // The Models
    3    // ==========
    4    // The User Model
    5    app.models.User = Force.SObject.extend({
    6        sobjectType: "User",
    7        fieldlist: ["Id", "FirstName", 
    8            "LastName", "SmallPhotoUrl", 
    9            "Title", "Email", 
    10            "MobilePhone","City"]
    11    });
    12
    13    // The UserCollection Model
    14    app.models.UserCollection = Force.SObjectCollection.extend({
    15        model: app.models.User
    16        fieldlist: ["Id", "FirstName", "LastName", 
    17            "SmallPhotoUrl", "Title"],
    18
    19        getCriteria: function() {
    20            return this.key;
    21        },
    22
    23        setCriteria: function(key) {
    24            this.key = key;
    25            this.config = {type:"soql", query:"SELECT " 
    26                + this.fieldlist.join(",")  
    27                + " FROM User"
    28                + " WHERE Name like '" + key + "%'"
    29                + " ORDER BY Name "
    30                + " LIMIT 25 "
    31            };
    32        }
    33    });
    34</script>

Example

Here’s the complete model code.
1<script>
2    // The Models
3
4    // The User Model
5    app.models.User = Force.SObject.extend({
6        sobjectType: "User",
7        fieldlist: ["Id", "FirstName", "LastName", 
8            "SmallPhotoUrl", "Title", "Email", 
9            "MobilePhone","City"]
10    });
11
12    // The UserCollection Model
13    app.models.UserCollection = Force.SObjectCollection.extend({
14        model: app.models.User
15        fieldlist: ["Id", "FirstName", "LastName", 
16            "SmallPhotoUrl", "Title"],
17
18        getCriteria: function() {
19            return this.key;
20        },
21
22        setCriteria: function(key) {
23            this.key = key;
24            this.config = {
25                type:"soql", 
26                query:"SELECT " + this.fieldlist.join(",")  
27                + " FROM User"
28                + " WHERE Name like '" + key + "%'"
29                + " ORDER BY Name "
30                + " LIMIT 25 "
31             };
32        }
33    });
34</script>