PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/builder/code/phreeze.backbone/scripts/model.js.tpl

http://github.com/jasonhinkle/phreeze
Smarty Template | 163 lines | 137 code | 26 blank | 0 comment | 25 complexity | 9b56e0e6f84418275bd5e8e01c484110 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. /**
  2. * backbone model definitions for {$appname}
  3. */
  4. /**
  5. * Use emulated HTTP if the server doesn't support PUT/DELETE or application/json requests
  6. */
  7. Backbone.emulateHTTP = false;
  8. Backbone.emulateJSON = false;
  9. var model = {};
  10. /**
  11. * long polling duration in miliseconds. (5000 = recommended, 0 = disabled)
  12. * warning: setting this to a low number will increase server load
  13. */
  14. model.longPollDuration = {if $enableLongPolling != '0'}5000{else}0{/if};
  15. /**
  16. * whether to refresh the collection immediately after a model is updated
  17. */
  18. model.reloadCollectionOnModelUpdate = true;
  19. /**
  20. * a default sort method for sorting collection items. this will sort the collection
  21. * based on the orderBy and orderDesc property that was used on the last fetch call
  22. * to the server.
  23. */
  24. model.AbstractCollection = Backbone.Collection.extend({
  25. totalResults: 0,
  26. totalPages: 0,
  27. currentPage: 0,
  28. pageSize: 0,
  29. orderBy: '',
  30. orderDesc: false,
  31. lastResponseText: null,
  32. lastRequestParams: null,
  33. collectionHasChanged: true,
  34. /**
  35. * fetch the collection from the server using the same options and
  36. * parameters as the previous fetch
  37. */
  38. refetch: function() {
  39. this.fetch({ data: this.lastRequestParams })
  40. },
  41. /* uncomment to debug fetch event triggers
  42. fetch: function(options) {
  43. this.constructor.__super__.fetch.apply(this, arguments);
  44. },
  45. // */
  46. /**
  47. * client-side sorting baesd on the orderBy and orderDesc parameters that
  48. * were used to fetch the data from the server. Backbone ignores the
  49. * order of records coming from the server so we have to sort them ourselves
  50. */
  51. comparator: function(a,b) {
  52. var result = 0;
  53. var options = this.lastRequestParams;
  54. if (options && options.orderBy) {
  55. // lcase the first letter of the property name
  56. var propName = options.orderBy.charAt(0).toLowerCase() + options.orderBy.slice(1);
  57. var aVal = a.get(propName);
  58. var bVal = b.get(propName);
  59. if (isNaN(aVal) || isNaN(bVal)) {
  60. // treat comparison as case-insensitive strings
  61. aVal = aVal ? aVal.toLowerCase() : '';
  62. bVal = bVal ? bVal.toLowerCase() : '';
  63. } else {
  64. // treat comparision as a number
  65. aVal = Number(aVal);
  66. bVal = Number(bVal);
  67. }
  68. if (aVal < bVal) {
  69. result = options.orderDesc ? 1 : -1;
  70. } else if (aVal > bVal) {
  71. result = options.orderDesc ? -1 : 1;
  72. }
  73. }
  74. return result;
  75. },
  76. /**
  77. * override parse to track changes and handle pagination
  78. * if the server call has returned page data
  79. */
  80. parse: function(response, options) {
  81. // the response is already decoded into object form, but it's easier to
  82. // compary the stringified version. some earlier versions of backbone did
  83. // not include the raw response so there is some legacy support here
  84. var responseText = options && options.xhr ? options.xhr.responseText : JSON.stringify(response);
  85. this.collectionHasChanged = (this.lastResponseText != responseText);
  86. this.lastRequestParams = options ? options.data : undefined;
  87. // if the collection has changed then we need to force a re-sort because backbone will
  88. // only resort the data if a property in the model has changed
  89. if (this.lastResponseText && this.collectionHasChanged) this.sort({ silent:true });
  90. this.lastResponseText = responseText;
  91. var rows;
  92. if (response.currentPage) {
  93. rows = response.rows;
  94. this.totalResults = response.totalResults;
  95. this.totalPages = response.totalPages;
  96. this.currentPage = response.currentPage;
  97. this.pageSize = response.pageSize;
  98. this.orderBy = response.orderBy;
  99. this.orderDesc = response.orderDesc;
  100. } else {
  101. rows = response;
  102. this.totalResults = rows.length;
  103. this.totalPages = 1;
  104. this.currentPage = 1;
  105. this.pageSize = this.totalResults;
  106. this.orderBy = response.orderBy;
  107. this.orderDesc = response.orderDesc;
  108. }
  109. return rows;
  110. }
  111. });
  112. {foreach from=$tables item=table}{if isset($tableInfos[$table->Name])}
  113. {assign var=singular value=$tableInfos[$table->Name]['singular']}
  114. {assign var=plural value=$tableInfos[$table->Name]['plural']}
  115. /**
  116. * {$singular} Backbone Model
  117. */
  118. model.{$singular}Model = Backbone.Model.extend({
  119. urlRoot: 'api/{$singular|lower}',
  120. idAttribute: '{$table->GetPrimaryKeyName()|studlycaps|lcfirst}',
  121. {foreach from=$table->Columns item=column name=columnsForEach}
  122. {$column->NameWithoutPrefix|studlycaps|lcfirst}: '',
  123. {/foreach}
  124. defaults: {
  125. {foreach from=$table->Columns item=column name=columnsForEach}
  126. '{$column->NameWithoutPrefix|studlycaps|lcfirst}': {if $column->NameWithoutPrefix == $table->GetPrimaryKeyName()}null{elseif $column->Type == "date" or $column->Type == "datetime"}new Date(){else}''{/if}{if !$smarty.foreach.columnsForEach.last},{/if}
  127. {/foreach}
  128. }
  129. });
  130. /**
  131. * {$singular} Backbone Collection
  132. */
  133. model.{$singular}Collection = model.AbstractCollection.extend({
  134. url: 'api/{$plural|lower}',
  135. model: model.{$singular}Model
  136. });
  137. {/if}{/foreach}