PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/www/js/index.js

https://bitbucket.org/bfagundez/sf_mobile_demo
JavaScript | 178 lines | 137 code | 34 blank | 7 comment | 3 complexity | bb8d524fbd96a7043d93d457fcb49ba0 MD5 | raw file
  1. // Backbone structure
  2. var SFDemoApp = {}
  3. SFDemoApp.DataCollection = Backbone.Collection.extend({});
  4. SFDemoApp.ListItemView = Backbone.View.extend({
  5. tagName: 'li',
  6. attributes: {'class':'ui-li ui-li-static ui-btn-up-a ui-last-child'},
  7. initialize:function(){
  8. this.template = "<h3 class='ui-li-heading'><%= rec.get('Name') %></h3><p class='ui-li-desc'><strong>Phone: <%= rec.get('Phone') %></strong></p>";
  9. },
  10. render: function(){
  11. var $el = $(this.el);
  12. $el.html(_.template(this.template,{ 'rec' :this.model } ));
  13. return this;
  14. }
  15. });
  16. SFDemoApp.ListView = Backbone.View.extend({
  17. initialize: function(){
  18. this.el = this.options.el;
  19. },
  20. render: function(){
  21. var $el = $(this.el);
  22. self = this;
  23. this.collection.each(function(list) {
  24. var item, sidebarItem;
  25. item = new SFDemoApp.ListItemView({ model: list });
  26. $el.append(item.render().el);
  27. });
  28. return this;
  29. }
  30. });
  31. // cordova start event
  32. document.addEventListener("deviceready", onDeviceReady, false);
  33. function login(uname,pwd){
  34. $.mobile.loading( 'show', { text: 'Logging in...', textVisible: true, theme: 'a'});
  35. var login_done = $.Deferred();
  36. var api_contacted = $.Deferred();
  37. var got_identity = $.Deferred();
  38. var SESSION_ID = '';
  39. var url_assembled = 'https://mobileportal.secure.force.com/soapapi__login?u='+uname+'&p='+pwd;
  40. // Login step 1, usr and pwd against login url.
  41. $.ajax({
  42. url:url_assembled,
  43. dataType:'jsonp'
  44. }).done(function(res){
  45. console.log('CALLOUT COMPLETED!')
  46. console.log(res);
  47. login_done.resolve(res);
  48. }).fail(function(res){
  49. navigator.notification.alert(
  50. 'There was an error contacting SF Portal', // message
  51. function(){},
  52. 'Exception found!', // title
  53. 'OK' // buttonName
  54. );
  55. });
  56. // Login step 2 , get api status
  57. $.when(login_done).done(function(res){
  58. console.log(api_contacted);
  59. console.log('Login completed!',res.sessionId,res);
  60. var sessionId = res.sessionId;
  61. SESSION_ID = res.sessionId
  62. // query for services
  63. console.log('calling sf to get service endpoints')
  64. $.ajax({
  65. url: 'https://na15.salesforce.com/services/data/v26.0/',
  66. beforeSend: function (request)
  67. {
  68. request.setRequestHeader("Authorization", 'Bearer '+sessionId );
  69. }, dataType: 'json'
  70. }).done(function(res){
  71. console.log(api_contacted);
  72. console.log('success',res);
  73. api_contacted.resolve(res);
  74. }).fail(function(res){
  75. console.log('fail',res);
  76. });
  77. });
  78. // Final step , display accounts
  79. $.when(api_contacted).done(function(){
  80. data = {}
  81. data.q = 'Select Id, Name, Phone from Account';
  82. $.ajax({
  83. url: 'https://na15.salesforce.com/services/data/v26.0/query/',
  84. data:data,
  85. dataType:'json',
  86. method: 'GET',
  87. beforeSend: function (request)
  88. {
  89. request.setRequestHeader("Authorization", 'Bearer '+SESSION_ID );
  90. }, dataType: 'json'
  91. }).done(function(res){
  92. console.log(res);
  93. console.log('success',res);
  94. var list = new SFDemoApp.ListView({collection:new SFDemoApp.DataCollection(res.records),el:$("#record_list")});
  95. console.log(list);
  96. list.render();
  97. $.mobile.changePage("#record_list_container", { 'transition' : 'flip'} );
  98. $.mobile.loading( 'hide' );
  99. }).fail(function(res){
  100. console.log('fail',res);
  101. });
  102. });
  103. }
  104. function onDeviceReady() {
  105. // Now safe to use the Cordova API
  106. console.log("Cordova is ready!");
  107. $("#signup_form_submit").click(function(){
  108. $.mobile.loading( 'show', { text: 'Registering...', textVisible: true, theme: 'a'});
  109. var url_assembled = 'https://mobileportal.secure.force.com/soapapi__register?u='+$('#u_field').val()+'&e='+$('#e_field').val()+'&p='+$('#p_field').val();
  110. console.log('Calling this url:',url_assembled);
  111. $.ajax({
  112. url:url_assembled,
  113. dataType:'jsonp'
  114. }).done(function(res){
  115. console.log('CALLOUT COMPLETED!')
  116. console.log(res);
  117. if(res.code == 200){
  118. $.mobile.changePage("#start");
  119. $.mobile.loading( 'hide' );
  120. $.mobile.changePage("#registration_success", { 'role' : 'dialog' , 'transition' : 'pop'} );
  121. $('#direct_login').click(function(){
  122. login($('#u_field').val(),$('#p_field').val());
  123. });
  124. } else {
  125. navigator.notification.alert(
  126. 'There was an error creating the user, try again.', // message
  127. function(){},
  128. 'Exception found!', // title
  129. 'OK' // buttonName
  130. );
  131. }
  132. }).fail(function(res){
  133. navigator.notification.alert(
  134. 'There was an error contacting SF Portal', // message
  135. function(){},
  136. 'Exception found!', // title
  137. 'OK' // buttonName
  138. );
  139. });
  140. });
  141. $("#login_form_submit").click(function(){
  142. login($('#u_login_field').val(),$('#p_login_field').val());
  143. });
  144. }