Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Allow configuring a prefix for localStorage keys
Browse files Browse the repository at this point in the history
  • Loading branch information
bergie committed Apr 18, 2013
1 parent 69419e3 commit 4a4586b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -63,6 +63,7 @@ Storage:
* new `save` and `saved` events are triggered for the whole set of entities being saved
* new `saveentity` and `savedentity` events are triggered for each entity being saved
* Options used when calling `saveRemote` are now passed on in the save-related events
* key prefix used with localStorage is now configurable

Create.js internals:

Expand Down
26 changes: 16 additions & 10 deletions examples/create.js
Expand Up @@ -179,7 +179,8 @@ See http://createjs.org for more information
vie: this.vie,
url: this.options.url,
localize: this.options.localize,
language: this.options.language
language: this.options.language,
storagePrefix: this.options.storagePrefix
});

var widget = this;
Expand Down Expand Up @@ -1754,6 +1755,8 @@ See http://createjs.org for more information
options: {
// Whether to use localstorage
localStorage: false,
// String prefix for localStorage identifiers
storagePrefix: '',
removeLocalstorageOnIgnore: true,
// VIE instance to use for storage handling
vie: null,
Expand Down Expand Up @@ -2119,7 +2122,8 @@ See http://createjs.org for more information
}
return this._saveLocalReferences(model.primaryCollection.subject, model.primaryCollection.predicate, model);
}
window.localStorage.setItem(model.getSubjectUri(), JSON.stringify(model.toJSONLD()));
var key = this.options.storagePrefix + model.getSubjectUri();
window.localStorage.setItem(key, JSON.stringify(model.toJSONLD()));
},

_getReferenceId: function (model, property) {
Expand All @@ -2136,7 +2140,7 @@ See http://createjs.org for more information
}

var widget = this;
var identifier = subject + ':' + predicate;
var identifier = this.options.storagePrefix + subject + ':' + predicate;
var json = model.toJSONLD();
if (window.localStorage.getItem(identifier)) {
var referenceList = JSON.parse(window.localStorage.getItem(identifier));
Expand All @@ -2157,7 +2161,8 @@ See http://createjs.org for more information
return false;
}

var local = window.localStorage.getItem(model.getSubjectUri());
var key = this.options.storagePrefix + model.getSubjectUri();
var local = window.localStorage.getItem(key);
if (!local) {
return false;
}
Expand All @@ -2169,8 +2174,8 @@ See http://createjs.org for more information
if (!this.options.localStorage) {
return false;
}

if (!window.localStorage.getItem(model.getSubjectUri())) {
var key = this.options.storagePrefix + model.getSubjectUri();
if (!window.localStorage.getItem(key)) {
return false;
}
return true;
Expand All @@ -2181,7 +2186,8 @@ See http://createjs.org for more information
return;
}

var local = window.localStorage.getItem(model.getSubjectUri());
var key = this.options.storagePrefix + model.getSubjectUri();
var local = window.localStorage.getItem(key);
if (!local) {
return;
}
Expand All @@ -2203,7 +2209,7 @@ See http://createjs.org for more information
return;
}

var identifier = this._getReferenceId(model, property);
var identifier = this.options.storagePrefix + this._getReferenceId(model, property);
var local = window.localStorage.getItem(identifier);
if (!local) {
return;
Expand Down Expand Up @@ -2243,8 +2249,8 @@ See http://createjs.org for more information
if (!this.options.localStorage) {
return;
}

window.localStorage.removeItem(model.getSubjectUri());
var key = this.options.storagePrefix + model.getSubjectUri();
window.localStorage.removeItem(key);
}
});
})(jQuery);
Expand Down
3 changes: 2 additions & 1 deletion src/jquery.Midgard.midgardCreate.js
Expand Up @@ -184,7 +184,8 @@
vie: this.vie,
url: this.options.url,
localize: this.options.localize,
language: this.options.language
language: this.options.language,
storagePrefix: this.options.storagePrefix
});

var widget = this;
Expand Down
23 changes: 14 additions & 9 deletions src/jquery.Midgard.midgardStorage.js
Expand Up @@ -15,6 +15,8 @@
options: {
// Whether to use localstorage
localStorage: false,
// String prefix for localStorage identifiers
storagePrefix: '',
removeLocalstorageOnIgnore: true,
// VIE instance to use for storage handling
vie: null,
Expand Down Expand Up @@ -380,7 +382,8 @@
}
return this._saveLocalReferences(model.primaryCollection.subject, model.primaryCollection.predicate, model);
}
window.localStorage.setItem(model.getSubjectUri(), JSON.stringify(model.toJSONLD()));
var key = this.options.storagePrefix + model.getSubjectUri();
window.localStorage.setItem(key, JSON.stringify(model.toJSONLD()));
},

_getReferenceId: function (model, property) {
Expand All @@ -397,7 +400,7 @@
}

var widget = this;
var identifier = subject + ':' + predicate;
var identifier = this.options.storagePrefix + subject + ':' + predicate;
var json = model.toJSONLD();
if (window.localStorage.getItem(identifier)) {
var referenceList = JSON.parse(window.localStorage.getItem(identifier));
Expand All @@ -418,7 +421,8 @@
return false;
}

var local = window.localStorage.getItem(model.getSubjectUri());
var key = this.options.storagePrefix + model.getSubjectUri();
var local = window.localStorage.getItem(key);
if (!local) {
return false;
}
Expand All @@ -430,8 +434,8 @@
if (!this.options.localStorage) {
return false;
}

if (!window.localStorage.getItem(model.getSubjectUri())) {
var key = this.options.storagePrefix + model.getSubjectUri();
if (!window.localStorage.getItem(key)) {
return false;
}
return true;
Expand All @@ -442,7 +446,8 @@
return;
}

var local = window.localStorage.getItem(model.getSubjectUri());
var key = this.options.storagePrefix + model.getSubjectUri();
var local = window.localStorage.getItem(key);
if (!local) {
return;
}
Expand All @@ -464,7 +469,7 @@
return;
}

var identifier = this._getReferenceId(model, property);
var identifier = this.options.storagePrefix + this._getReferenceId(model, property);
var local = window.localStorage.getItem(identifier);
if (!local) {
return;
Expand Down Expand Up @@ -504,8 +509,8 @@
if (!this.options.localStorage) {
return;
}

window.localStorage.removeItem(model.getSubjectUri());
var key = this.options.storagePrefix + model.getSubjectUri();
window.localStorage.removeItem(key);
}
});
})(jQuery);

0 comments on commit 4a4586b

Please sign in to comment.