Permalink
Browse files

Updated to modern standards.

  • Loading branch information...
1 parent e448dd5 commit 7678aa3e161eb6014d4fad65341b5544fd337746 @elfsternberg committed Aug 7, 2011
Showing with 1,169 additions and 9,110 deletions.
  1. +0 −84 backbone-localstorage.js
  2. +334 −191 backbone.js
  3. +408 −118 backbonestore.nw
  4. +6 −0 data/items.json
  5. +66 −64 index.html
  6. +0 −8,176 jquery-1.5.js
  7. +18 −0 jquery-1.6.2.min.js
  8. +0 −1 jquery.tmpl.min.js
  9. +0 −283 jquery.validjson.js
  10. +136 −85 store.js
  11. +0 −24 underscore-min.js
  12. +201 −84 underscore.js
@@ -1,84 +0,0 @@
-// A simple module to replace `Backbone.sync` with *localStorage*-based
-// persistence. Models are given GUIDS, and saved into a JSON object. Simple
-// as that.
-
-// Generate four random hex digits.
-function S4() {
- return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
-};
-
-// Generate a pseudo-GUID by concatenating random hexadecimal.
-function guid() {
- return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
-};
-
-// Our Store is represented by a single JS object in *localStorage*. Create it
-// with a meaningful name, like the name you'd give a table.
-var Store = function(name) {
- this.name = name;
- var store = localStorage.getItem(this.name);
- this.data = (store && JSON.parse(store)) || {};
-};
-
-_.extend(Store.prototype, {
-
- // Save the current state of the **Store** to *localStorage*.
- save: function() {
- localStorage.setItem(this.name, JSON.stringify(this.data));
- },
-
- // Add a model, giving it a (hopefully)-unique GUID, if it doesn't already
- // have an id of it's own.
- create: function(model) {
- if (!model.id) model.id = model.attributes.id = guid();
- this.data[model.id] = model;
- this.save();
- return model;
- },
-
- // Update a model by replacing its copy in `this.data`.
- update: function(model) {
- this.data[model.id] = model;
- this.save();
- return model;
- },
-
- // Retrieve a model from `this.data` by id.
- find: function(model) {
- return this.data[model.id];
- },
-
- // Return the array of all models currently in storage.
- findAll: function() {
- return _.values(this.data);
- },
-
- // Delete a model from `this.data`, returning it.
- destroy: function(model) {
- delete this.data[model.id];
- this.save();
- return model;
- }
-
-});
-
-// Override `Backbone.sync` to use delegate to the model or collection's
-// *localStorage* property, which should be an instance of `Store`.
-Backbone.sync = function(method, model, success, error) {
-
- var resp;
- var store = model.localStorage || model.collection.localStorage;
-
- switch (method) {
- case "read": resp = model.id ? store.find(model) : store.findAll(); break;
- case "create": resp = store.create(model); break;
- case "update": resp = store.update(model); break;
- case "delete": resp = store.destroy(model); break;
- }
-
- if (resp) {
- success(resp);
- } else {
- error("Record not found");
- }
-};
Oops, something went wrong.

0 comments on commit 7678aa3

Please sign in to comment.