global.__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('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({});
            }
            dirty.set(
                getUrl(model),
                model.toJSON(),
                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);
        }
    };
};


@@ -1162,21 +1162,46 @@
     
-return data ?
+if (data) %7B%0A                    return
  suc
@@ -1209,42 +1209,133 @@
 ess(
+_(
 data)
- : error('Model not found.');
+.clone());%0A                %7D else %7B%0A                    return error(new Error('Model not found.'));%0A                %7D
 %0A   
@@ -1450,16 +1450,20 @@
         
+if (
 val && k
@@ -1492,35 +1492,56 @@
  && 
+!_(
 data
+)
 .in
-dexOf(val) === -1 &&
+clude(val)) %7B%0A                       
  dat
@@ -1551,13 +1551,46 @@
 ush(
+_(
 val)
-;
+.clone());%0A                    %7D
 %0A   

Set resp to be clone of node-dirty entry rather than reference.