Guest User

Untitled

a guest
Jul 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. // ==========================================================================
  2. // Project: Spanish.WordDataSource
  3. // Copyright: ©2011 My Company, Inc.
  4. // ==========================================================================
  5. /*globals Spanish */
  6.  
  7. /** @class
  8.  
  9. (Document Your Data Source Here)
  10.  
  11. @extends SC.DataSource
  12. */
  13. sc_require('models/word');
  14. Spanish.WORDS_QUERY = SC.Query.local(Spanish.Word,{
  15. orderBy: 'word'
  16. });
  17.  
  18. Spanish.WordDataSource = SC.DataSource.extend(
  19. /** @scope Spanish.WordDataSource.prototype */ {
  20.  
  21. // ..........................................................
  22. // QUERY SUPPORT
  23. //
  24.  
  25. fetch: function(store, query) {
  26. if(query === Spanish.WORDS_QUERY){
  27. SC.Request.getUrl('/words').header({'Accept': 'application/json'}).json()
  28. .notify(this, 'didFetchWords', store, query)
  29. .send();
  30. return YES;
  31. }
  32.  
  33. return NO ; // return YES if you handled the query
  34. },
  35.  
  36. didFetchWords: function(response, store, query){
  37. if(SC.ok(response)){
  38. var storeKeys = store.loadRecords(Spanish.Word, response.get('body'));
  39. store.loadQueryResults(query, storeKeys);
  40. }else{
  41. store.dataSourceDidErrorQuery(query, response);
  42. }
  43. },
  44.  
  45. // ..........................................................
  46. // RECORD SUPPORT
  47. //
  48.  
  49. retrieveRecord: function(store, storeKey) {
  50. if(SC.kindOf(store.recordTypeFor(storeKey), Spanish.Word)){
  51. var url = '/words/' + store.idFor(storeKey);
  52. SC.Request.get(url).header({
  53. 'Accept':'application/json'
  54. }).json()
  55. .notify(this, 'didRetrieveWord', store, storeKey)
  56. .send();
  57. return YES;
  58. }else{
  59. return NO ; // return YES if you handled the storeKey
  60.  
  61. }
  62.  
  63. },
  64.  
  65. didRetrieveWord: function(response, store, storeKey){
  66. if(SC.ok(response)){
  67. var dataHash = response.get('body').content;
  68. store.dataSourceDidComplete(storeKey, dataHash);
  69. }else{
  70. store.dataSourceDidError(storeKey, response);
  71. }
  72. },
  73.  
  74. createRecord: function(store, storeKey) {
  75. if (SC.kindOf(store.recordTypeFor(storeKey), Spanish.Word)) {
  76.  
  77. SC.Request.postUrl('/words').header({
  78. 'Accept': 'application/json'
  79. }).json()
  80. .notify(this, "didCreateWord", store, storeKey)
  81. .send(store.readDataHash(storeKey));
  82. return YES;
  83.  
  84. } else{
  85. return NO;
  86. }
  87. },
  88.  
  89. didCreateWord: function(response, store, storeKey) {
  90. if (SC.ok(response)) {
  91. // Adapted from parseUri 1.2.2
  92. // (c) Steven Levithan <stevenlevithan.com>
  93. // MIT License
  94. var parser = /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
  95. var url = parser.exec(response.header('Location'))[8];
  96. store.dataSourceDidComplete(storeKey, null, url); // update url
  97.  
  98. } else store.dataSourceDidError(storeKey, response);
  99. },
  100.  
  101.  
  102. // ..........................................................
  103. // UPDATE RECORDS
  104. //
  105.  
  106. updateRecord: function(store, storeKey) {
  107. if (SC.kindOf(store.recordTypeFor(storeKey), Spanish.Word)) {
  108. SC.Request.putUrl(store.idFor(storeKey)).header({
  109. 'Accept': 'application/json'
  110. }).json()
  111. .notify(this, this.didUpdateWord, store, storeKey)
  112. .send(store.readDataHash(storeKey));
  113. return YES;
  114.  
  115. } else return NO ;
  116. },
  117. didUpdateWord: function(response, store, storeKey) {
  118. if (SC.ok(response)) {
  119. var data = response.get('body');
  120. if (data) data = data.content; // if hash is returned; use it.
  121. store.dataSourceDidComplete(storeKey, data) ;
  122.  
  123. } else store.dataSourceDidError(storeKey);
  124. },
  125.  
  126. // ..........................................................
  127. // DESTROY RECORDS
  128. //
  129.  
  130. destroyRecord: function(store, storeKey) {
  131. if (SC.kindOf(store.recordTypeFor(storeKey), Spanish.Word)) {
  132. SC.Request.deleteUrl(store.idFor(storeKey)).header({
  133. 'Accept': 'application/json'
  134. }).json()
  135. .notify(this, this.didDestroyWord, store, storeKey)
  136. .send();
  137. return YES;
  138.  
  139. } else return NO;
  140. },
  141. didDestroyWord: function(response, store, storeKey) {
  142. if (SC.ok(response)) {
  143. store.dataSourceDidDestroy(storeKey);
  144. } else store.dataSourceDidError(response);
  145. }
  146. }) ;
Add Comment
Please, Sign In to add comment