PageRenderTime 24ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

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