PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/backbone-indexdb.js

https://github.com/podviaznikov/backbone-indexdb
JavaScript | 63 lines | 61 code | 0 blank | 2 comment | 2 complexity | ad7613d7061a1a2a2beb8e094b89868c MD5 | raw file
  1. // (c) 2011 Enginimation Studio (http://enginimation.com).
  2. // backbone-indexdb.js may be freely distributed under the MIT license.
  3. "use strict";
  4. Porridge.Model = Backbone.Model.extend(
  5. {
  6. initialize:function()
  7. {
  8. if(!this.get('id'))
  9. {
  10. this.id=UUID.generate();
  11. this.set({id:this.id});
  12. }
  13. },
  14. save:function()
  15. {
  16. var entityName = this.constructor.definition.name;
  17. Porridge.save(entityName,this.toJSON(),this.id);
  18. },
  19. destroy:function()
  20. {
  21. var entityName = this.constructor.definition.name;
  22. var keyName = this.constructor.definition.key||'id';
  23. var model = this;
  24. var success=function()
  25. {
  26. model.trigger('destroy', model, model.collection);
  27. }
  28. Porridge.remove(entityName,this.get(keyName),success);
  29. }
  30. },
  31. {
  32. definition:{}
  33. });
  34. Porridge.Collection = Backbone.Collection.extend(
  35. {
  36. fetch:function(options)
  37. {
  38. options || (options = {});
  39. var collection = this;
  40. var entityName = this.model.definition.name;
  41. var addOne = function(data)
  42. {
  43. collection.add(new collection.model({attributes:data}));
  44. }
  45. Porridge.all(entityName,addOne,function()
  46. {
  47. collection.trigger('retrieved');
  48. },options.error);
  49. },
  50. fetchByKey:function(keyName,keyValue)
  51. {
  52. var collection = this;
  53. var entityName = this.model.definition.name;
  54. var addOne = function(data)
  55. {
  56. collection.add(new collection.model({attributes:data}));
  57. }
  58. Porridge.allByKey(entityName,keyName,keyValue,addOne,function()
  59. {
  60. collection.trigger('retrieved');
  61. });
  62. }
  63. });