PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/pollapli/ui/static/pollapli/models/updates_models.js

https://bitbucket.org/kaosat_dev/pollapli
JavaScript | 136 lines | 117 code | 10 blank | 9 comment | 10 complexity | 927e8c9169cbc0a79dcf81c69fa67ffb MD5 | raw file
  1. function is(type, obj)
  2. {
  3. var clas = Object.prototype.toString.call(obj).slice(8, -1);
  4. return obj !== undefined && obj !== null && clas === type;
  5. }
  6. var UpdateStatus = Backbone.Model.extend(
  7. {
  8. });
  9. var Update = Backbone.Model.extend(
  10. {
  11. urlRoot : pollapli.mainUrl+'rest/config/updates/1',
  12. initialize: function()
  13. {
  14. /*
  15. var data={"start":true}
  16. data=JSON.stringify(data);
  17. this.postData(manager.mainUrl+"rest/config/updates/"+updateId+"/status",'application/pollapli.update.status+json',data,
  18. */
  19. },
  20. defaults:
  21. {
  22. id:-1,
  23. type:null,
  24. name: 'an update',
  25. description: 'description',
  26. version:'0.0.0',
  27. downloaded:false,
  28. installed:false,
  29. img: '',
  30. tags:''
  31. }
  32. });
  33. var Updates = Backbone.Collection.extend(
  34. {
  35. model : Update,
  36. url: pollapli.mainUrl+'rest/config/updates',
  37. parse: function(response)
  38. {
  39. //alert(JSON.stringify(response.updates.items));
  40. return response.updates.items;
  41. },
  42. comparator : function(update)
  43. {
  44. return update.get("type");
  45. },
  46. sortByParam : function(param)
  47. {
  48. var result = _(this.sortBy(function(update)
  49. {
  50. //workeround for inverted compare (includes handling of strings)
  51. if( param.indexOf("-") != -1)
  52. {
  53. tmp=update.get(param.substr(1));
  54. if(is('String', tmp))
  55. {
  56. return -tmp.charCodeAt(0);
  57. }
  58. else
  59. {
  60. return -tmp
  61. }
  62. }
  63. else
  64. {
  65. return update.get(param);
  66. }
  67. }));
  68. return result;
  69. },
  70. filterByParam : function(params)
  71. {
  72. //need a look at the "all" underscore js method
  73. var result= _(this.filter(function(update)
  74. {
  75. valid=true;
  76. _.each(params, function(value, key)
  77. {
  78. console.log("update: "+update.get("name")+ " key: "+key +" update var value: "+update.get(key)+" val: "+value);
  79. if(update.get(key)!=value)
  80. {
  81. valid=false;
  82. }
  83. });
  84. return valid;
  85. }));
  86. return result;
  87. },
  88. filterAndOrder : function(filterParams,sortParam)
  89. {
  90. var result=_(this.chain()
  91. .sortBy(function(update)
  92. {
  93. //workaround for inverted compare (includes handling of strings)
  94. if( sortParam.indexOf("-") != -1)
  95. {
  96. tmp=update.get(sortParam.substr(1));
  97. if(is('String', tmp))
  98. {
  99. return -tmp.charCodeAt(0);
  100. }
  101. else
  102. {
  103. return -tmp
  104. }
  105. }
  106. else
  107. {
  108. return update.get(sortParam);
  109. }
  110. })
  111. .filter(function(update)
  112. {
  113. valid=true;
  114. _.each(filterParams, function(value, key)
  115. {
  116. if(update.get(key)!=value )
  117. {
  118. valid=false;
  119. }
  120. });
  121. return valid;
  122. }));
  123. result=result.value();
  124. return result
  125. }
  126. });