Guest User

Untitled

a guest
Oct 16th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. // this sets the background color of the master UIView (when there are no windows/tab groups on it)
  2. Titanium.UI.setBackgroundColor('#000');
  3.  
  4. var Cloud = require('ti.cloud');
  5. Cloud.debug = true;
  6.  
  7. var win = Ti.UI.createWindow({
  8. title:"ACS Custom Object Example",
  9. backgroundColor:'#ccc'
  10. })
  11.  
  12. var createUserBtn = Ti.UI.createButton({
  13. title:'Create User',
  14. width:110,
  15. top:20,
  16. left:20,
  17. height:35,
  18. textAlign:'center'
  19. })
  20. win.add(createUserBtn);
  21.  
  22. createUserBtn.addEventListener('click', function () {
  23. //creating sample cloud user
  24. Cloud.Users.create({
  25. username: "testuser1",
  26. password: "testuser1",
  27. password_confirmation: "testuser1",
  28. first_name: "test",
  29. last_name: "user1"
  30. }, function (e) {
  31. if (e.success) {
  32. var user = e.users[0];
  33. alert('Created! You are now logged in as ' + user.id);
  34.  
  35. }
  36. else {
  37. if (e.error && e.message) {
  38. alert('Error :' + e.message);
  39. }
  40. }
  41.  
  42. });
  43. });
  44.  
  45.  
  46. var cusObjBtn1 = Ti.UI.createButton({
  47. title:'Custom Object 1',
  48. width:130,
  49. top:60,
  50. left:20,
  51. height:35,
  52. textAlign:'center'
  53. })
  54. win.add(cusObjBtn1);
  55.  
  56. cusObjBtn1.addEventListener('click', function(e) {
  57. Cloud.Users.login({
  58. login : 'testuser1',
  59. password: 'testuser1',
  60. }, function(e) {
  61. if (e.success) {
  62.  
  63. Cloud.Objects.create({
  64. classname : 'books',
  65. fields : {
  66. book_id : 1,
  67. title : 'Javascript',
  68. author : 'Peter'
  69. }
  70. }, function(e) {
  71. if(e.success) {
  72. alert("created");
  73. } else {
  74. alert('Error: ' + ((e.error && e.message) || JSON.stringify(e)));
  75. }
  76. });
  77.  
  78. } else {
  79. alert('Login Error:' +((e.error && e.message) || JSON.stringify(e)));
  80. }
  81. });
  82. });
  83.  
  84. var cusObjBtn2 = Ti.UI.createButton({
  85. title:'Custom Object 2',
  86. width:130,
  87. top:60,
  88. left:155,
  89. height:35,
  90. textAlign:'center'
  91. })
  92. win.add(cusObjBtn2);
  93.  
  94. cusObjBtn2.addEventListener('click', function(e) {
  95. Cloud.Users.login({
  96. login : 'testuser1',
  97. password: 'testuser1',
  98. }, function(e) {
  99. if (e.success) {
  100.  
  101. Cloud.Objects.create({
  102. classname : 'books',
  103. fields : {
  104. book_id : 3,
  105. title : 'PHP',
  106. author : 'John'
  107. }
  108. }, function(e) {
  109. if(e.success) {
  110. alert("created");
  111. } else {
  112. alert('Create Error:' + ((e.error && e.message) || JSON.stringify(e)));
  113. }
  114. });
  115.  
  116. } else {
  117. alert('Login Error:' +
  118. ((e.error && e.message) || JSON.stringify(e)));
  119. }
  120. });
  121. });
  122.  
  123. var scrollView = Titanium.UI.createScrollView({
  124. backgroundColor:'#fff',
  125. contentWidth:300,
  126. contentHeight:"auto",
  127. showVerticalScrollIndicator:true,
  128. showHorizontalScrollIndicator:true,
  129. borderRadius:5,
  130. borderWidth:1,
  131. borderColor:'#999',
  132. top:145,
  133. left:10,
  134. right:10,
  135. bottom:10
  136. });
  137. win.add(scrollView);
  138.  
  139. var opLabel = Ti.UI.createLabel({
  140. backgroundColor:'#fff',
  141. left:5,
  142. top:5,
  143. right:5,
  144. font:{fontSize:15},
  145. color:'#000'
  146. })
  147. scrollView.add(opLabel);
  148.  
  149.  
  150. // Create a Button.
  151. var Query = Ti.UI.createButton({
  152. title : 'Submit Query',
  153. height : 35,
  154. width : 130,
  155. top : 100,
  156. left:20
  157. });
  158. win.add(Query);
  159.  
  160.  
  161. var dialog = Titanium.UI.createOptionDialog({
  162. title: 'Query Options',
  163. options: ["All","WHERE author = 'Peter'", "WHERE book_id > 2", "Cancel" ],
  164. cancel:3
  165. });
  166.  
  167. dialog.addEventListener('click', function(e) {
  168. if(e.index == 0){
  169. var params = {classname: "books",page: 1,per_page: 10};
  170. }else if(e.index == 1){
  171. var params = {classname: "books",page: 1,per_page: 10, where: { author: "Peter"}};
  172. }else if(e.index == 2){
  173. var params = {classname: "books",page: 1,per_page: 10, where: { "book_id":{"$gte" : 2}}};
  174. }
  175.  
  176. displayResult(params)
  177.  
  178. });
  179. // Listen for click events.
  180.  
  181. Query.addEventListener('click', function(e){
  182. dialog.show();
  183. });
  184.  
  185.  
  186. function displayResult(params){
  187. Cloud.Objects.query(
  188. params
  189. , function(e) {
  190. if (e.success) {
  191. opLabel.text = JSON.stringify(e.books);
  192. } else {
  193. alert('Error: ' +((e.error && e.message) || JSON.stringify(e)));
  194. }
  195. })
  196.  
  197. }
  198.  
  199. win.open();
Add Comment
Please, Sign In to add comment