sf_mobile_demo /www/js/index.js

Language Javascript Lines 179
MD5 Hash bb8d524fbd96a7043d93d457fcb49ba0
Repository https://bitbucket.org/bfagundez/sf_mobile_demo.git View Raw File
| Open JSFiddle
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Backbone structure
var SFDemoApp = {}
SFDemoApp.DataCollection = Backbone.Collection.extend({});
SFDemoApp.ListItemView = Backbone.View.extend({
  tagName: 'li',
  attributes: {'class':'ui-li ui-li-static ui-btn-up-a ui-last-child'},
  initialize:function(){
    this.template = "<h3 class='ui-li-heading'><%= rec.get('Name') %></h3><p class='ui-li-desc'><strong>Phone: <%= rec.get('Phone') %></strong></p>";
  },
  render: function(){
    var $el = $(this.el);
    $el.html(_.template(this.template,{ 'rec' :this.model } ));
    return this;
  }
});

SFDemoApp.ListView = Backbone.View.extend({
  initialize: function(){
    this.el = this.options.el;
  },
  render: function(){

    var $el = $(this.el);
    self = this;

    this.collection.each(function(list) {
                         var item, sidebarItem;
                         item = new SFDemoApp.ListItemView({ model: list });
                         $el.append(item.render().el);
                      });

    return this;

  }
});

// cordova start event
document.addEventListener("deviceready", onDeviceReady, false);

function login(uname,pwd){

    $.mobile.loading( 'show', { text: 'Logging in...', textVisible: true, theme: 'a'});

    var login_done = $.Deferred();
    var api_contacted = $.Deferred();
    var got_identity = $.Deferred();
    var SESSION_ID = '';
    var url_assembled = 'https://mobileportal.secure.force.com/soapapi__login?u='+uname+'&p='+pwd;

    // Login step 1, usr and pwd against login url.
    $.ajax({
           url:url_assembled,
           dataType:'jsonp'
           }).done(function(res){
                   console.log('CALLOUT COMPLETED!')
                   console.log(res);
                   login_done.resolve(res);

                   }).fail(function(res){
                           navigator.notification.alert(
                                                        'There was an error contacting SF Portal',  // message
                                                        function(){},
                                                        'Exception found!',            // title
                                                        'OK'                  // buttonName
                                                        );
                           });

    // Login step 2 , get api status
    $.when(login_done).done(function(res){
                            console.log(api_contacted);
                            console.log('Login completed!',res.sessionId,res);
                            var sessionId = res.sessionId;
                            SESSION_ID = res.sessionId
                            // query for services
                            console.log('calling sf to get service endpoints')
                            $.ajax({
                                   url: 'https://na15.salesforce.com/services/data/v26.0/',
                                   beforeSend: function (request)
                                   {
                                   request.setRequestHeader("Authorization", 'Bearer '+sessionId );
                                   }, dataType: 'json'
                                   }).done(function(res){
                                           console.log(api_contacted);
                                           console.log('success',res);
                                           api_contacted.resolve(res);
                                           }).fail(function(res){
                                                   console.log('fail',res);
                                                   });

                            });


    // Final step , display accounts
    $.when(api_contacted).done(function(){
                               data = {}
                               data.q = 'Select Id, Name, Phone from Account';
                               $.ajax({
                                      url: 'https://na15.salesforce.com/services/data/v26.0/query/',
                                      data:data,
                                      dataType:'json',
                                      method: 'GET',
                                      beforeSend: function (request)
                                      {
                                      request.setRequestHeader("Authorization", 'Bearer '+SESSION_ID );
                                      }, dataType: 'json'
                                      }).done(function(res){
                                              console.log(res);
                                              console.log('success',res);
                                              var list = new SFDemoApp.ListView({collection:new SFDemoApp.DataCollection(res.records),el:$("#record_list")});
                                              console.log(list);
                                              list.render();
                                              $.mobile.changePage("#record_list_container", { 'transition' : 'flip'} );
                                              $.mobile.loading( 'hide' );
                                              }).fail(function(res){
                                                      console.log('fail',res);
                                                      });
                               });



}

function onDeviceReady() {
    // Now safe to use the Cordova API
    console.log("Cordova is ready!");

    $("#signup_form_submit").click(function(){

                                   $.mobile.loading( 'show', { text: 'Registering...', textVisible: true, theme: 'a'});

                                   var url_assembled = 'https://mobileportal.secure.force.com/soapapi__register?u='+$('#u_field').val()+'&e='+$('#e_field').val()+'&p='+$('#p_field').val();

                                   console.log('Calling this url:',url_assembled);
                                   $.ajax({
                                          url:url_assembled,
                                          dataType:'jsonp'
                                          }).done(function(res){
                                                  console.log('CALLOUT COMPLETED!')
                                                  console.log(res);

                                                  if(res.code == 200){
                                                  $.mobile.changePage("#start");
                                                  $.mobile.loading( 'hide' );
                                                  $.mobile.changePage("#registration_success", { 'role' : 'dialog' , 'transition' : 'pop'} );
                                                  $('#direct_login').click(function(){
                                                                           login($('#u_field').val(),$('#p_field').val());
                                                                           });

                                                  } else {
                                                  navigator.notification.alert(
                                                                               'There was an error creating the user, try again.',  // message
                                                                               function(){},
                                                                               'Exception found!',            // title
                                                                               'OK'                  // buttonName
                                                                               );
                                                  }

                                                  }).fail(function(res){
                                                          navigator.notification.alert(
                                                                                       'There was an error contacting SF Portal',  // message
                                                                                       function(){},
                                                                                       'Exception found!',            // title
                                                                                       'OK'                  // buttonName
                                                                                       );
                                                          });
                                   });


    $("#login_form_submit").click(function(){
      login($('#u_login_field').val(),$('#p_login_field').val());
    });
}
Back to Top