PageRenderTime 58ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/pollapli/ui/static/pollapli/models/baseCollection.js

https://bitbucket.org/kaosat_dev/pollapli
JavaScript | 52 lines | 50 code | 1 blank | 1 comment | 5 complexity | dc044915117469d8a0889f97c82a994e MD5 | raw file
  1. var BaseCollection = Backbone.Collection.extend(
  2. {
  3. filterAndOrder : function(filterParams,sortParam)
  4. {
  5. var result=_(this.chain()
  6. .sortBy(function(node)
  7. {
  8. //workaround for inverted compare (includes handling of strings)
  9. if( sortParam.indexOf("-") != -1)
  10. {
  11. tmp=node.get(sortParam.substr(1));
  12. if(is('String', tmp))
  13. {
  14. tmp=tmp.toLowerCase();
  15. return -tmp.charCodeAt(0);
  16. }
  17. else
  18. {
  19. return -tmp
  20. }
  21. }
  22. else
  23. {
  24. tmp=node.get(sortParam)
  25. if(is('String', tmp))
  26. {
  27. tmp=tmp.toLowerCase();
  28. return tmp.charCodeAt(0);
  29. }
  30. else
  31. {
  32. return tmp
  33. }
  34. }
  35. })
  36. .filter(function(node)
  37. {
  38. valid=true;
  39. _.each(filterParams, function(value, key)
  40. {
  41. if(node.get(key)!=value )
  42. {
  43. valid=false;
  44. }
  45. });
  46. return valid;
  47. }));
  48. result=result.value();
  49. return result
  50. }
  51. });