Guest User

Untitled

a guest
Jun 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. // ==========================================================================
  2. // Project: Blog.NewsDataSource
  3. // Copyright: ©2010 My Company, Inc.
  4. // ==========================================================================
  5. /*globals Blog */
  6.  
  7. /** @class
  8.  
  9. (Document Your Data Source Here)
  10.  
  11. @extends SC.DataSource
  12. */
  13. sc_require('models/news');
  14. Blog.NEWS_QUERY = SC.Query.local(Blog.News, {orderBy: 'id' });
  15. Blog.NewsDataSource = SC.DataSource.extend(
  16. /** @scope Blog.NewsDataSource.prototype */ {
  17.  
  18. // ..........................................................
  19. // QUERY SUPPORT
  20. //
  21.  
  22. fetch: function(store, query) {
  23. if (query === Blog.NEWS_QUERY) {
  24. SC.Request.getUrl('/news.json').header({
  25. 'Accept': 'application/json'
  26. }).json().notify(this, 'didFetchQuery', store, query).send();
  27. return YES;
  28. }
  29. return NO;
  30. },
  31.  
  32. // ..........................................................
  33. // RECORD SUPPORT
  34. //
  35.  
  36. didFetchQuery: function(response, store, query) {
  37. var storeKeys = store.loadRecords(Blog.News,
  38. Blog.TaskJSONProxy.normalize_task_data(response.get('body')));
  39. if(SC.ok(response)) {
  40. console.debug('didFetchTasks');
  41.  
  42. store.loadRecords(query.primaryKey, response.get('body'));
  43. store.dataSourceDidFetchQuery(query);
  44. } else store.dataSourceDidErrorQuery(query, response);
  45. },
  46.  
  47. retrieveRecord: function(store, storeKey) {
  48. console.debug('retrieveRecord');
  49. if (SC.kindOf(store.recordTypeFor(storeKey), Blog.News)) {
  50.  
  51. var url = store.idFor(storeKey);
  52. console.log(url);
  53. SC.Request.getUrl(url).header({
  54. 'Accept': 'application/json'
  55. }).json()
  56. .notify(this, 'didRetrieveTask', store, storeKey)
  57. .send();
  58.  
  59. return YES;
  60.  
  61. } else return NO;
  62. },
  63. didRetrieveTask: function(response, store, storeKey) {
  64. if (SC.ok(response)) {
  65. console.debug('true');
  66. var dataHash = response.get('body');
  67. store.dataSourceDidComplete(storeKey, dataHash);
  68.  
  69. } else store.dataSourceDidError(storeKey, response);
  70. },
  71.  
  72. createRecord: function(store, storeKey) {
  73. if (SC.kindOf(store.recordTypeFor(storeKey), Blog.News)) {
  74.  
  75. SC.Request.postUrl('/news.json').header({
  76. 'Accept': 'application/json'
  77. }).json()
  78. .notify(this, this.didCreateTask, store, storeKey)
  79. .send(store.readDataHash(storeKey));
  80. return YES;
  81.  
  82. } else return NO;
  83. },
  84. didCreateTask: function(response, store, storeKey) {
  85. if (SC.ok(response)) {
  86. // Adapted from parseUri 1.2.2
  87. // (c) Steven Levithan <stevenlevithan.com>
  88. // MIT License
  89. var parser = /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
  90. var url = parser.exec(response.header('Location'))[8];
  91. store.dataSourceDidComplete(storeKey, null, url); // update url
  92.  
  93. } else store.dataSourceDidError(storeKey, response);
  94. },
  95.  
  96. updateRecord: function(store, storeKey) {
  97. if (SC.kindOf(store.recordTypeFor(storeKey), Blog.News)) {
  98. SC.Request.putUrl(store.idFor(storeKey)).header({
  99. 'Accept': 'application/json'
  100. }).json()
  101. .notify(this, this.didUpdateTask, store, storeKey)
  102. .send(store.readDataHash(storeKey));
  103. return YES;
  104.  
  105. } else return NO ;
  106. },
  107. didUpdateTask: function(response, store, storeKey) {
  108. if (SC.ok(response)) {
  109. //var data = response.get('body');
  110. //if (data) data = data.content; // if hash is returned; use it.
  111. store.dataSourceDidComplete(storeKey,response.get('body')) ;
  112.  
  113. } else store.dataSourceDidError(storeKey);
  114. },
  115.  
  116. destroyRecord: function(store, storeKey) {
  117.  
  118. // TODO: Add handlers to destroy records on the data source.
  119. // call store.dataSourceDidDestroy(storeKey) when done
  120.  
  121. return NO ; // return YES if you handled the storeKey
  122. },
  123. _urlFor: function(resourceName, id) {
  124. if(id) {
  125. return '/app/' + resourceName + 's/' + id + '.json';
  126. } else {
  127. return '/app/' + resourceName + 's.json';
  128. }
  129. }
  130.  
  131. }) ;
Add Comment
Please, Sign In to add comment