Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*jslint vars: true, unparam: true, white: true, browser: true */
- /*global jQuery, tiddlyweb */
- tiddlyweb.Store = (function($) {
- "use strict";
- // NB: assumes all tiddlers belong to the same host
- var Store = function(tiddlers) {
- this.tiddlers = {};
- this.titles = {}; // XXX: rename (misleading!)
- var self = this;
- $.each(tiddlers || [], function(i, tid) {
- self.add(tid);
- });
- };
- // NB: assumes tiddler has a bag
- Store.prototype.add = function(tid) {
- var id = tid.bag.name + "/" + tid.title;
- this.tiddlers[id] = tid;
- // index tiddlers by title
- this.titles[tid.title] = this.titles[tid.title] || [];
- this.titles[tid.title].push(tid);
- };
- Store.prototype.get = function(id) {
- return this.tiddlers[id];
- };
- Store.prototype.save = function() {
- var id;
- for(id in this.tiddlers) {
- if(this.tiddlers.hasOwnProperty(id)) {
- var json = this.tiddlers[id].serialize();
- localStorage.setItem(id, json); // TODO: use namespace?
- }
- }
- };
- Store.prototype.load = function() {
- var i;
- for(i = 0; i < localStorage.length; i += 1) {
- var key = localStorage.key(i);
- var json = localStorage.getItem(key);
- var tid = new tiddlyweb.Tiddler.deserialize(json);
- this.add(tid);
- }
- };
- return Store;
- }(jQuery));
Add Comment
Please, Sign In to add comment