PageRenderTime 29ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/sites/web/bower_components/localforage-backbone/src/localforage.backbone.js

https://bitbucket.org/aswinvk28/smartpan-stock-drupal
JavaScript | 183 lines | 118 code | 17 blank | 48 comment | 27 complexity | 28983bfe2707f0c468adaba91a5d170a MD5 | raw file
Possible License(s): LGPL-2.1
  1. // backbone.localforage allows users of Backbone.js to store their collections
  2. // entirely offline with no communication to a REST server. It uses whatever
  3. // driver localForage is set to use to store the data (IndexedDB, WebSQL, or
  4. // localStorage, depending on availability). This allows apps on Chrome,
  5. // Firefox, IE, and Safari to use async, offline storage, which is cool.
  6. //
  7. // The basics of how to use this library is that it lets you override the
  8. // `sync` method on your collections and models to use localForage. So
  9. //
  10. // var MyModel = Backbone.Model.extend({})
  11. // var MyCollection = Backbone.Collection.extend({
  12. // model: MyModel
  13. // });
  14. //
  15. // becomes
  16. //
  17. // var MyModel = Backbone.Collection.extend({
  18. // sync: Backbone.localforage.sync('ModelNamespace')
  19. // });
  20. // var MyCollection = Backbone.Collection.extend({
  21. // model: MyModel,
  22. // sync: Backbone.localforage.sync('MyCollection')
  23. // });
  24. //
  25. // Inspiration for this file comes from a few backbone.localstorage
  26. // implementations.
  27. (function (root, factory) {
  28. if (typeof define === 'function' && define.amd) {
  29. define(['localforage', 'backbone', 'underscore'], factory);
  30. } else if (typeof module !== 'undefined' && module.exports) {
  31. var localforage = require('localforage');
  32. var Backbone = require('backbone');
  33. var _ = require('underscore');
  34. module.exports = factory(localforage, Backbone, _);
  35. } else {
  36. factory(root.localforage, root.Backbone, root._);
  37. }
  38. }(this, function (localforage, Backbone, _) {
  39. function S4() {
  40. return ((1 + Math.random()) * 65536 | 0).toString(16).substring(1);
  41. }
  42. function guid() {
  43. return S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4();
  44. }
  45. // For now, we aren't complicated: just set a property off Backbone to
  46. // serve as our export point.
  47. Backbone.localforage = {
  48. sync: function(name) {
  49. var _this = this;
  50. var sync = function(method, model, options) {
  51. // If `this` is a `Backbone.Collection` it means
  52. // `Backbone.Collection#fetch` has been called.
  53. if (this instanceof Backbone.Collection) {
  54. // If there's no localforageKey for this collection, create
  55. // it.
  56. if (!this.sync.localforageKey) {
  57. this.sync.localforageKey = name;
  58. }
  59. } else { // `this` is a `Backbone.Model` if not a `Backbone.Collection`.
  60. // Generate an id if one is not set yet.
  61. // TODO: Fix this to use `Backbone.Model#idAttribute`.
  62. if (!model.id) {
  63. model.id = model.attributes.id = guid();
  64. }
  65. // If there's no localforageKey for this model create it
  66. if (!model.sync.localforageKey) {
  67. model.sync.localforageKey = name + "/" + model.id;
  68. }
  69. }
  70. switch (method) {
  71. case "read":
  72. return model.id ? _this.find(model, options) : _this.findAll(model, options);
  73. case "create":
  74. return _this.create(model, options);
  75. case "update":
  76. return _this.update(model, options);
  77. case "delete":
  78. return _this.destroy(model, options);
  79. }
  80. };
  81. // This needs to be exposed for later usage, but it's private to
  82. // the adapter.
  83. sync._localforageNamespace = name;
  84. return sync;
  85. },
  86. save: function(model, callback) {
  87. localforage.setItem(model.sync.localforageKey, model.toJSON(), function(data) {
  88. // If this model has a collection, keep the collection in =
  89. // sync as well.
  90. if (model.collection) {
  91. var collection = model.collection;
  92. // Create an array of `model.collection` models' ids.
  93. var collectionData = collection.map(function(model) {
  94. return collection.model.prototype.sync._localforageNamespace + '/' + model.id;
  95. });
  96. // Bind `data` to `callback` to call after
  97. // `model.collection` models' ids are persisted.
  98. callback = callback ? _.partial(callback, data) : void 0;
  99. // Persist `model.collection` models' ids.
  100. localforage.setItem(model.collection.sync.localforageKey, collectionData, callback);
  101. } else if (callback) {
  102. callback(data);
  103. }
  104. });
  105. },
  106. create: function(model, callbacks) {
  107. // We always have an ID available by this point, so we just call
  108. // the update method.
  109. return this.update(model, callbacks);
  110. },
  111. update: function(model, callbacks) {
  112. this.save(model, function(data) {
  113. if (callbacks.success) {
  114. callbacks.success(data);
  115. }
  116. });
  117. },
  118. find: function(model, callbacks) {
  119. localforage.getItem(model.sync.localforageKey, function(data) {
  120. if (!_.isEmpty(data)) {
  121. if (callbacks.success) {
  122. callbacks.success(data);
  123. }
  124. } else if (callbacks.error) {
  125. callbacks.error();
  126. }
  127. });
  128. },
  129. // Only used by `Backbone.Collection#sync`.
  130. findAll: function(collection, callbacks) {
  131. localforage.getItem(collection.sync.localforageKey, function(data) {
  132. if (data && data.length) {
  133. var done = function () {
  134. if (callbacks.success) {
  135. callbacks.success(data);
  136. }
  137. };
  138. // Only execute `done` after getting all of the
  139. // collection's models.
  140. done = _.after(data.length, done);
  141. var onModel = function(i, model) {
  142. data[i] = model;
  143. done();
  144. };
  145. for (var i = 0; i < data.length; ++i) {
  146. localforage.getItem(data[i], _.partial(onModel, i));
  147. }
  148. } else {
  149. data = [];
  150. if (callbacks.success) {
  151. callbacks.success(data);
  152. }
  153. }
  154. });
  155. },
  156. destroy: function(model, callbacks) {
  157. localforage.removeItem(model.sync.localforageKey, function() {
  158. var json = model.toJSON();
  159. if (callbacks.success) {
  160. callbacks.success(json);
  161. }
  162. });
  163. }
  164. };
  165. return Backbone.localforage;
  166. }));