Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit ec646bd9f775d95e8c67462809fabc2adb4b8f64 @siuying committed Jan 6, 2012
Showing with 200 additions and 0 deletions.
  1. +50 −0 README.md
  2. +67 −0 backbone.memory.coffee
  3. +83 −0 backbone.memory.js
@@ -0,0 +1,50 @@
+# Backbone memory sync adapter v0.1.0
+
+Simplistic memory-only sync adapter for Backbone. It's a drop-in replacement for Backbone.Sync() to handle saving to memory.
+
+Use for testing or volatile data that need no persistence.
+
+## Usage
+
+Include backbone.memory.js after having included Backbone.js:
+
+ <script type="text/javascript" src="backbone.js"></script>
+ <script type="text/javascript" src="backbone.memory.js"></script>
+
+Create your collections like so:
+
+ window.SomeCollection = Backbone.Collection.extend({
+ storage: new Store("SomeCollection"), // Unique name within your app.
+
+ // ... everything else is normal.
+
+ });
+
+Feel free to use Backbone as you usually would, this is a drop-in replacement.
+
+## Credits
+
+Heavily borrowed from [Backbone.localStorage](https://github.com/jeromegn/Backbone.localStorage)
+
+## Licensed
+
+Licensed under MIT license
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,67 @@
+# ##UTILS
+# function for generating "random" id of objects in DB
+S4 = ->
+ return (((1+Math.random())*0x10000)|0).toString(16).substring(1)
+
+guid = ->
+ return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
+
+# ## Memory Store
+# a backend storage that work only in memory
+class MemoryStore
+ constructor: (@db) ->
+ @store = {}
+
+ create: (model) ->
+ # when you want use your id as identifier, use apiid attribute
+ if model.attributes.apiid
+ model.id = model.attributes.id = model.attributes.apiid
+ else
+ model.id = model.attributes.id = guid()
+ @store[model.id] = model
+
+ update: (model) ->
+ @store[model.id] = model if model.id
+
+ destroy: (model) ->
+ id = model.id || model.attributes.id
+ @store[id] = undefined
+
+ find: (model) ->
+ id = model.id || model.attributes.id
+ @store[id]
+
+ findAll: (model) ->
+ all = (value for key, value of @store)
+
+# Backbone.sync with memory
+Backbone.memorySync = (method, model, options) ->
+ store = model.store || model.collection.store
+
+ unless store
+ console.warn "[BACKBONE-MEMORY] model without store object -> ", model
+ return
+
+ result = switch method
+ when "read"
+ if model.id
+ store.find(model)
+ else
+ store.findAll(model)
+ when "create"
+ store.create(model)
+ when "update"
+ store.update(model)
+ when "delete"
+ store.destroy(model)
+ else
+ nil
+
+ if result
+ options.success(result)
+
+# Override 'Backbone.sync' to default to memorySync,
+# the original 'Backbone.sync' is still available in 'Backbone.ajaxSync'
+window.Store = MemoryStore
+Backbone.ajaxSync = Backbone.sync
+Backbone.sync = Backbone.memorySync
@@ -0,0 +1,83 @@
+var MemoryStore, S4, guid;
+S4 = function() {
+ return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
+};
+guid = function() {
+ return S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4();
+};
+MemoryStore = (function() {
+ function MemoryStore(db) {
+ this.db = db;
+ this.store = {};
+ }
+ MemoryStore.prototype.create = function(model) {
+ if (model.attributes.apiid) {
+ model.id = model.attributes.id = model.attributes.apiid;
+ } else {
+ model.id = model.attributes.id = guid();
+ }
+ return this.store[model.id] = model;
+ };
+ MemoryStore.prototype.update = function(model) {
+ if (model.id) {
+ return this.store[model.id] = model;
+ }
+ };
+ MemoryStore.prototype.destroy = function(model) {
+ var id;
+ id = model.id || model.attributes.id;
+ return this.store[id] = void 0;
+ };
+ MemoryStore.prototype.find = function(model) {
+ var id;
+ id = model.id || model.attributes.id;
+ return this.store[id];
+ };
+ MemoryStore.prototype.findAll = function(model) {
+ var all, key, value;
+ return all = (function() {
+ var _ref, _results;
+ _ref = this.store;
+ _results = [];
+ for (key in _ref) {
+ value = _ref[key];
+ _results.push(value);
+ }
+ return _results;
+ }).call(this);
+ };
+ return MemoryStore;
+})();
+Backbone.memorySync = function(method, model, options) {
+ var result, store;
+ store = model.store || model.collection.store;
+ if (!store) {
+ console.warn("[BACKBONE-MEMORY] model without store object -> ", model);
+ return;
+ }
+ result = (function() {
+ switch (method) {
+ case "read":
+ if (model.id) {
+ return store.find(model);
+ } else {
+ return store.findAll(model);
+ }
+ break;
+ case "create":
+ return store.create(model);
+ case "update":
+ return store.update(model);
+ case "delete":
+ return store.destroy(model);
+ default:
+ return nil;
+ }
+ })();
+ if (result) {
+ return options.success(result);
+ }
+};
+window.Store = MemoryStore;
+Backbone.ajaxSync = Backbone.sync;
+Backbone.sync = Backbone.memorySync;

0 comments on commit ec646bd

Please sign in to comment.