jdb - a javascript object manager
Wed, 21 February 2018
work-in-progress.html
<script> var options = { basePath: "/admin/test", headers: {}, objects: { 'default': [ { name: "foo", id: "bar" } ] } }; var jdb = (function(options){ var defaultNamespace = 'default'; var defaults = { basePath: window.location.pathname, headers: {}, objects: { defaultNamespace: {} } }; var objects = {}; // Object containing 'namespace': [{obj1}, {obj2}, {obj3}] var namespaces = []; // List of namespace names. for (var n in options){ defaults[n]=options[n]; } this.getObjects = function(ns) { ns = 'undefined' ? defaultNamespace: ns; if (namespaces.indexOf(ns) === -1) { return []; } return objects[ns]; }; this.getObject = function(id, ns) { ns = 'undefined' ? defaultNamespace : ns; if (namespaces.indexOf(ns) === -1) { return null; } for (var i in objects[ns]) { if (objects[ns][i]['id'] === id) { return objects[ns]; } } return null; }; this.getNamespaces = function() { return namespaces }; this.addObject = function(obj, namespace) { if (typeof obj['id'] === 'undefined') { throw "jdb.addObject requires objects contain a id key."; } namespace = 'undefined' ? defaultNamespace: namespace; if (typeof objects[namespace] === 'undefined') { objects[namespace] = []; } obj['__jdb_ns'] = namespace; objects[namespace].push(obj); }; for (var ns in defaults.objects) { namespaces.push(ns); for (var obj_i = 0; obj_i < defaults.objects[ns].length; obj_i++) { addObject(defaults.objects[ns][obj_i]); } } if (namespaces.indexOf(defaultNamespace) === -1) { namespaces.push(defaultNamespace); objects[defaultNamespace] = []; } return this; }); var inst = jdb(options); inst.addObject( { id: "12345", name: "Foo" } ); inst.getObject('12345'); </script>