Skip to content

Instantly share code, notes, and snippets.

@netsi1964
Created November 18, 2012 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netsi1964/4104607 to your computer and use it in GitHub Desktop.
Save netsi1964/4104607 to your computer and use it in GitHub Desktop.
javascript simple web app state reflection using location - QueryStringSettings
var QueryStringSettings = {
selector: '[ng-model]:not([type="checkbox"])',
doneCallback: null,
set: function() {
var h = '';
[].forEach.call(document.querySelectorAll(this.selector), function(e) {
h += ((h !== '') ? '/' : '') + e.id + '=' + e.value;
});
document.location.hash = h;
if (this.doneCallback!==null) {
this.doneCallback.call(this, 'set', h);
}
},
get: function(scope) {
// scope is the object containing the web app settings
// made to integrate with AngularJS
var settings = [];
[].forEach.call(location.hash.split('/'), function(e) {
var a = e.split('=');
if (a.length === 2) {
settings.push(a);
if (typeof scope==='undefined') {
document.querySelector('#' + a[0]).value = a[1];
} else {
scope[a[0]] = a[1];
}
}
});
if (this.doneCallback!==null) {
this.doneCallback.call(this, 'get', settings);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment