// A quick test implementation to get something happening on the screen.
// a simple in-memory data adapter for tests.
var ns = new (require('Nonsense'))();

this.testData = {};

this.findModel = function(model, id) {
    var dfr = new _.Deferred();

    if (!this.testData[model] || !id) {
        dfr.reject(405, 'no model or id');
    } else {
        var result = _(this.testData[model]).findWhere({id:id});
        result ? dfr.resolve(result) : dfr.reject(404);
    }

    return dfr.promise();
};

_.extend(this, {
    readModel: function readModel(model, id) {
        debug('readModel :', model, id);
        return this.findModel(model, id); 
    },
    updateModel: function readModel(model, id, data) {
        debug('updateModel :', model, id);
        var dfr = new _.Deferred();

        var data = _.clone(data);

        function updateModel(m) {
            _.extend(m, data);
            dfr.resolve(m);
        }
        this.findModel(model, id).then(updateModel, dfr.reject);

        return dfr.promise();
    },
    createModel: function(model, data) {
        debug('createModel :', model, data);
        var dfr = new _.Deferred();

        var data = _.clone(data);

        function modelExists(model) {
            dfr.reject(409, 'Conflict');
        }

        // we dont want the model to exist
        function createModel(err, reason) {
            _.extend(data, { id: ns.uuid() });
            this.testData[model].push(data);
            dfr.resolve(data);
        }

        this.findModel(model, data.id).then(modelExists, _.bind(createModel, this));

        return dfr.promise();
    },
    deleteModel: function(model, id) {
        var dfr = new _.Deferred();

        function deleteModel(m) {
            var ind = _(this.testData[model]).indexOf(m);
            this.testData[model].splice(ind, 1);

            dfr.resolve(204);
        }

        this.findModel(model, id).then(_.bind(deleteModel, this), dfr.reject);
        return dfr.promise();
    },
    readCollection: function readModel(col) {
        debug('read collection ' + col);
        var dfr = new _.Deferred();
        this.testData[col] ? dfr.resolve(this.testData[col]) : dfr.reject(404);
        return dfr.promise();
    },
    setupData: function() {
        Graft.Data.reqres.setHandler('read', this.readModel, this);
        Graft.Data.reqres.setHandler('update', this.updateModel, this);
        Graft.Data.reqres.setHandler('create', this.createModel, this);
        Graft.Data.reqres.setHandler('delete', this.deleteModel, this);
        Graft.Data.reqres.setHandler('query', this.readCollection, this);
    }
});

this.addInitializer(function(opts) {
    debug("adding handler for reading models");
    Graft.Data.commands.setHandler('setup', this.setupData, this);
});

@@ -541,32 +541,38 @@
 ction readModel(
+name, 
 model, id) %7B%0A   
@@ -589,37 +589,36 @@
 ('readModel :', 
-model
+name
 , id);%0A        r
@@ -634,29 +634,28 @@
 s.findModel(
-model
+name
 , id); %0A    
@@ -689,24 +689,30 @@
 n readModel(
+name, 
 model, id, d
@@ -749,21 +749,20 @@
 del :', 
-model
+name
 , id);%0A 
@@ -950,37 +950,36 @@
  this.findModel(
-model
+name
 , id).then(updat
@@ -1059,24 +1059,30 @@
 l: function(
+name, 
 model, data)
@@ -1115,21 +1115,20 @@
 del :', 
-model
+name
 , data);
@@ -1450,21 +1450,20 @@
 estData%5B
-model
+name
 %5D.push(d
@@ -1533,21 +1533,20 @@
 ndModel(
-model
+name
 , data.i
@@ -1650,32 +1650,38 @@
 Model: function(
+name, 
 model, id) %7B%0A   
@@ -1786,21 +1786,20 @@
 estData%5B
-model
+name
 %5D).index
@@ -1831,21 +1831,20 @@
 estData%5B
-model
+name
 %5D.splice
@@ -1918,21 +1918,20 @@
 ndModel(
-model
+name
 , id).th
@@ -2049,16 +2049,22 @@
 adModel(
+name, 
 col) %7B%0A 
@@ -2097,19 +2097,20 @@
 ion ' + 
-col
+name
 );%0A     
@@ -2162,19 +2162,20 @@
 estData%5B
-col
+name
 %5D ? dfr.
@@ -2196,19 +2196,20 @@
 estData%5B
-col
+name
 %5D) : dfr

move to using the Data.read etc. methods.