pax_global_header00006660000000000000000000000064115741634030014516gustar00rootroot0000000000000052 comment=815a1f96a1be55f668182b6e63ae82e865fc38a8 developmentseed-backbone-dirty-815a1f9/000077500000000000000000000000001157416340300201345ustar00rootroot00000000000000developmentseed-backbone-dirty-815a1f9/LICENSE.md000066400000000000000000000027141157416340300215440ustar00rootroot00000000000000Copyright (c), Development Seed All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name "Development Seed" nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. developmentseed-backbone-dirty-815a1f9/README.md000066400000000000000000000022201157416340300214070ustar00rootroot00000000000000Backbone Dirty -------------- Server-side overrides for Backbone to use `node-dirty` for Model persistence. ### Compatibility Backbone 0.3.3. ### Usage Pass a filepath to the db (will be created if it doesn't exist yet) when calling `require()`. var Backbone = require('backbone'); Backbone.sync = require('backbone-dirty')('app.db').sync; // Backbone.sync will now load and save models from app.db. ### Conventions `backbone-dirty` stores models in the `node-dirty` db using the `model.url` as its key. Collections retrieve models by matching the Collection url against the initial portion of the Model url. var orange = new FruitModel({id: 'orange'}); var apple = new FruitModel({id: 'apple'}); var banana = new FruitModel({id: 'banana'}); console.log(orange.url()); // fruits/orange console.log(apple.url()); // fruits/apple console.log(banana.url()); // fruits/banana var fruits = new FruitCollection(); console.log(fruits.url); // fruits fruits.fetch(); // retrieves orange, apple, banana ### Authors - [Will White](http://github.com/willwhite) - [Young Hahn](http://github.com/yhahn) developmentseed-backbone-dirty-815a1f9/backbone-dirty.js000066400000000000000000000054041157416340300233720ustar00rootroot00000000000000global.__backbone_dirty__ || (global.__backbone_dirty__ = {}); // Provides a `Backbone.sync` or `Model.sync` method for the server-side // context. Uses `node-dirty` for model persistence. Models are expected to // have a URL prefixed by their respective collection (e.g. `/{class}/{id}`) // and Collections retrieve their respective models based on this convention. var _ = require('underscore'); var dbs = global.__backbone_dirty__; module.exports = function(filename) { var dirty = dbs[filename] = dbs[filename] || require('dirty')(filename); // Helper function to get a URL from a Model or Collection as a property // or as a function. var getUrl = function(object) { if (object.url instanceof Function) { return object.url(); } else if (typeof object.url === 'string') { return object.url; } }; // Sync implementation for `node-dirty`. var sync = function(method, model, success, error) { switch (method) { case 'read': var data, base = getUrl(model); if (model.id) { data = dirty.get(base); return data ? success(data) : error(new Error('Model not found.')); } else { data = []; dirty.forEach(function(key, val) { val && key.indexOf(base) === 0 && data.indexOf(val) === -1 && data.push(val); }); return success(data); } break; case 'create': case 'update': if (_.isEqual(dirty.get(getUrl(model)), model.toJSON())) { return success({}); } var data = _(dirty.get(getUrl(model)) || {}).extend(model.toJSON()); dirty.set( getUrl(model), data, function(err) { return err ? error(err) : success({}); } ); break; case 'delete': if (typeof dirty.get(getUrl(model)) === 'undefined') { return success({}); } dirty.rm( getUrl(model), function(err) { return err ? error(err) : success({}); } ); break; } }; // Set a loaded flag to indicate whether sync can begin accessing // the db immediately or must wait until the `load` event is emitted. dirty.on('load', function() { dbs[filename].loaded = true }); return { dirty: dirty, sync: function(method, model, success, error) { var deferred = function() { sync(method, model, success, error) }; dbs[filename].loaded ? deferred() : dirty.on('load', deferred); } }; }; developmentseed-backbone-dirty-815a1f9/package.json000066400000000000000000000005051157416340300224220ustar00rootroot00000000000000{ "name": "backbone-dirty", "version": "1.1.2", "author": "Development Seed (http://developmentseed.org)", "dependencies": { "underscore": "1.1.x", "dirty": "0.9.x" }, "licenses": [{ "type": "BSD" }], "main": "./backbone-dirty", "scripts": { "test": "expresso" } } developmentseed-backbone-dirty-815a1f9/test/000077500000000000000000000000001157416340300211135ustar00rootroot00000000000000developmentseed-backbone-dirty-815a1f9/test/test.js000066400000000000000000000116731157416340300224400ustar00rootroot00000000000000// Write the DB fixture. var fs = require('fs'), db = '{"key":"/api/Fruits/apple","val":{"id":"apple","name":"McIntosh Apple"}}\n' + '{"key":"/api/Fruits/melon","val":{"id":"melon","name":"Cantaloupe"}}\n' + '{"key":"/api/Veggies/broccoli","val":{"id":"broccoli","name":"Broccoli"}}\n'; fs.writeFileSync(__dirname + '/test.db', db); var _ = require('underscore'), assert = require('assert'), sync = require('../backbone-dirty')(__dirname + '/test.db').sync; // Prop up skeletal Backbone models -- only the methods we need. var Model = { toJSON: function() { return this; } }; var APPLE = _({ id: 'apple', url: function() { return '/api/Fruits/apple'; } }).extend(Model); var BROCCOLI = _({ id: 'broccoli', url: function() { return '/api/Veggies/broccoli'; } }).extend(Model); var BANANA = _({ id: 'banana', name: 'Yellow Banana', url: function() { return '/api/Fruits/banana'; } }).extend(Model); var MELON = _({ id: 'melon', url: function() { return '/api/Fruits/melon'; } }).extend(Model); var ORANGE = _({ id: 'orange', url: function() { return '/api/Fruits/orange'; } }).extend(Model); var FRUITS = _({ url: function() { return '/api/Fruits'; } }).extend(Model); // Initial read check. exports['read'] = function() { sync('read', ORANGE, function success(resp) { assert.ok(false, 'read: `orange` response should be error.'); }, function error(resp) { assert.ok(resp instanceof Error, 'read: `orange` response should be error.'); } ); sync('read', APPLE, function success(resp) { assert.equal(resp.name, 'McIntosh Apple', 'read: `apple` successfully.'); }, function error(resp) { assert.ok(resp instanceof Error, 'read: `apple` successfully.'); } ); }; // Read a collection. exports['collection'] = function() { sync('read', FRUITS, function success(resp) { assert.deepEqual(_(resp).pluck('id'), ['apple', 'melon'], 'read: `fruits` contains only fruits'); }, function error(resp) { assert.ok(false, 'read: `fruits` contains only fruits'); } ); }; // Full crud cycle. exports['create'] = function(beforeExit) { var methods = { 'create': function() { sync('create', BANANA, function success(resp) { assert.deepEqual(resp, {}, 'create: `banana` response should be {}.'); methods.cRead(); }, function error(resp) { assert.ok(false, 'create: `banana` response should be {}.'); } ); }, 'cRead': function() { sync('read', BANANA, function success(resp) { assert.equal(resp.name, 'Yellow Banana', 'create: `banana` read successfully.'); methods.update(); }, function error(resp) { assert.ok(false, 'create: `banana` read successfully.'); } ); }, 'update': function() { sync('update', _(_(BANANA).clone()).extend({ name: 'Brown Banana' }), function success(resp) { assert.deepEqual(resp, {}, 'update: `banana` response should be {}.'); methods.uRead(); }, function error(resp) { assert.ok(false, 'update: `banana` response should be {}.'); } ); }, 'uRead': function() { sync('read', BANANA, function success(resp) { assert.equal(resp.name, 'Brown Banana', 'update: `banana` read successfully.'); methods.delete(); }, function error(resp) { assert.ok(false, 'update: `banana` read successfully.'); } ); }, 'delete': function() { sync('delete', BANANA, function success(resp) { assert.deepEqual(resp, {}, 'delete: `banana` response should be {}.'); methods.dRead(); }, function error(resp) { assert.ok(false, 'delete: `banana` response should be {}.'); } ); }, 'dRead': function() { sync('reread', BANANA, function success(resp) { assert.deepEqual(false, 'reread: `banana` should return error.'); }, function error(resp) { assert.ok(true, 'reread: `banana` should return error.'); } ); } }; methods.create(); // Clean up fixtures db. beforeExit(function() { fs.unlinkSync(__dirname + '/test.db'); }); };