PageRenderTime 62ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/generator/views/modelcollections.js

http://github.com/jspears/bobamo
JavaScript | 125 lines | 110 code | 10 blank | 5 comment | 32 complexity | dfbc051e1fa0800eb90b2f248fa37cd6 MD5 | raw file
Possible License(s): Apache-2.0
  1. define([
  2. 'underscore',
  3. 'Backbone'
  4. ], function(_, Backbone) {
  5. "use strict";
  6. //we define these together because they often link together and if they are in seperate callbacks bad things happen.
  7. var schema //${nl()} = {{json model.schemaFor(model.fieldsets || model.edit_fields)}};
  8. var defaults//${nl()} = {{json model.defaults || {} }};
  9. var Model = Backbone.Model.extend({
  10. urlRoot:'${api}/${urlRoot}',
  11. schema:schema,
  12. defaults:defaults,
  13. initialize: function() {},
  14. parse:function(resp) {
  15. console.log('/${api}/${model.modelName}model#parse', resp);
  16. var fix = resp.payload ? resp.payload : resp
  17. return _.isArray(fix) ? fix[0] : fix;
  18. },
  19. get:function(key){
  20. if (key && key.indexOf('.') > -1){
  21. var split = key.split('.');
  22. var val = this.attributes;
  23. while (split.length && val)
  24. val = val[split.shift()];
  25. return val;
  26. }
  27. return Backbone.Model.prototype.get.call(this, key);
  28. },
  29. labelAttr:"${model.labelAttr}",
  30. toString:function(){
  31. if (this.labelAttr)
  32. return this.get(this.labelAttr);
  33. return Backbone.Model.prototype.toString.call(this);
  34. }
  35. });
  36. var Collection = Backbone.Collection.extend({
  37. model: Model,
  38. url:'${api}/${urlRoot}',
  39. initialize: function() {
  40. this.total = -1;
  41. },
  42. parse:function(resp) {
  43. console.log('Collection#parse ${model.modelName}', resp.payload);
  44. this.total = resp.total;
  45. return resp.payload ? resp.payload : resp;
  46. },
  47. _sorted:function(callback){
  48. if (!this.params){
  49. this.params = { limit:10, skip:0}
  50. }
  51. if (!this.params.sort)
  52. this.params.sort = '_id:1';
  53. console.log('params', this.params)
  54. this.fetch({data:_.omit(this.params, 'sorts'), success:callback});
  55. },
  56. nextId:function(callback){
  57. var current = this.currentId || this.models.length && this.models[0].id;
  58. if (current){
  59. var model = this.get(current);
  60. var pos =model && this.models.indexOf(model);
  61. if (pos > -1){
  62. if ( pos+1 < this.models.length ){
  63. return callback(this.at(pos+1).id);
  64. }
  65. }
  66. if (this.params && this.total && this.params.skip < this.total){
  67. //searched but didn't find, this is tricky, its my best guess.
  68. if (this.params)
  69. this.params.skip += this.params.limit;
  70. }
  71. }
  72. //could not find it anywhere
  73. if (this.params && this.params.skip && this.params.skip > this.total){
  74. return callback(null);
  75. }
  76. this._sorted(_.bind(this.nextId, this, callback));
  77. },
  78. previousId:function(callback){
  79. var current = this.currentId || this.models.length && this.models[0].id;
  80. if (current){
  81. var model = this.get(current);
  82. var pos =model && this.models.indexOf(model) - 1;
  83. if (pos > -1){
  84. return callback(this.at(pos).id);
  85. }
  86. if (this.params && this.total && this.params.skip > 0){
  87. //searched but didn't find, this is tricky, its my best guess.
  88. if (this.params)
  89. this.params.skip = Math.max(this.params.skip - this.params.limit, 0);
  90. }
  91. }
  92. //could not find it anywhere
  93. if (this.params && this.params.skip && this.params.skip <= 0){
  94. return callback(null);
  95. }
  96. this._sorted(_.bind(this.previousId, this, callback));
  97. },
  98. search:function(q, success, failure){
  99. this.fetch({
  100. data:{
  101. filter:{
  102. '${model.labelAttr}':q
  103. }
  104. },
  105. processJson:true,
  106. success:success,
  107. failure:failure
  108. })
  109. }
  110. });
  111. return {
  112. Model:Model,
  113. Collection:Collection
  114. };
  115. });