PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/README.md

https://github.com/Fer0x/Backbone.Safe
Markdown | 59 lines | 48 code | 11 blank | 0 comment | 0 complexity | 0a60ead0af5f7bab4b85587ee1c8a52a MD5 | raw file
Possible License(s): MIT
  1. # Backbone.Safe - local storage plugin
  2. Backbone.Safe is a plugin for Backbone.js which stores a model's or a collection's json data to the local storage on set operations, regardless server side existance.
  3. The concept for Backbone.Safe has been created while the developing the [Echoes Player project](https://github.com/orizens/echoes).
  4. ## Install
  5. Using Bower:
  6. ```
  7. bower install backbone.safe
  8. ```
  9. ## Usage
  10. The latest update to Backbone.Safe allows one to define safe with a non distructive definition using a key as such:
  11. ```javascript
  12. var UserProfile = Backbone.Model.extend({
  13. safe: {
  14. key: 'myAppUserSafe',
  15. reload: false
  16. },
  17. initialize: function() {
  18. // some statements;
  19. this.safe.reload()
  20. },
  21. fetchRoles: function() {
  22. // some statements;
  23. }
  24. });
  25. var user = new UserProfile();
  26. console.log( user.safe );
  27. ```
  28. With this method, when Safe is not present, the model/collection still functions as expected.
  29. On the other hand, Safe can defined using a factory method as such:
  30. ```javascript
  31. var HistoryPlaylist = Backbone.Collection.extend({
  32. model: someModel,
  33. initialize: function() {
  34. Backbone.Safe.create('historyPlaylist', this);
  35. },
  36. queue: function(youtubeJSON) {
  37. this.add(youtubeJSON);
  38. }
  39. });
  40. var myPlaylist = new HistoryPlaylist();
  41. console.log( myPlaylist.safe );
  42. ```
  43. ### Useful Information
  44. 1. Currently supports only one level of models/collections.
  45. 2. For Model: listens to a 'change' events and stores the data.
  46. 3. For Collection: listens to 'add', 'reset' events and stores data.
  47. 4. The 'create' function creates a new instance of Backbone.Safe in the 'safe' property
  48. [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/orizens/backbone.safe/trend.png)](https://bitdeli.com/free "Bitdeli Badge")