Guest User

Untitled

a guest
Jan 23rd, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. /*jslint vars: true, unparam: true, white: true, browser: true */
  2. /*global jQuery, tiddlyweb */
  3.  
  4. tiddlyweb.Store = (function($) {
  5.  
  6. "use strict";
  7.  
  8. // NB: assumes all tiddlers belong to the same host
  9. var Store = function(tiddlers) {
  10. this.tiddlers = {};
  11. this.titles = {}; // XXX: rename (misleading!)
  12. var self = this;
  13. $.each(tiddlers || [], function(i, tid) {
  14. self.add(tid);
  15. });
  16. };
  17. // NB: assumes tiddler has a bag
  18. Store.prototype.add = function(tid) {
  19. var id = tid.bag.name + "/" + tid.title;
  20. this.tiddlers[id] = tid;
  21. // index tiddlers by title
  22. this.titles[tid.title] = this.titles[tid.title] || [];
  23. this.titles[tid.title].push(tid);
  24. };
  25. Store.prototype.get = function(id) {
  26. return this.tiddlers[id];
  27. };
  28. Store.prototype.save = function() {
  29. var id;
  30. for(id in this.tiddlers) {
  31. if(this.tiddlers.hasOwnProperty(id)) {
  32. var json = this.tiddlers[id].serialize();
  33. localStorage.setItem(id, json); // TODO: use namespace?
  34. }
  35. }
  36. };
  37. Store.prototype.load = function() {
  38. var i;
  39. for(i = 0; i < localStorage.length; i += 1) {
  40. var key = localStorage.key(i);
  41. var json = localStorage.getItem(key);
  42. var tid = new tiddlyweb.Tiddler.deserialize(json);
  43. this.add(tid);
  44. }
  45. };
  46.  
  47. return Store;
  48.  
  49. }(jQuery));
Add Comment
Please, Sign In to add comment