// A simple module to replace `Backbone.sync` with a fixed JSON data set in appData.js


// Our Store is represented by a single JS object appData.js called 'itemsData'
var Store = function(name, data) {
  this.name = name;
  this.data = data;
};

_.extend(Store.prototype, {

  // 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);
  }

});

// Override `Backbone.sync` to use delegate to the model or collection's store
Backbone.sync = function(method, model, options) {

  var resp;
  var store = model.localStorage || model.collection.localStorage;

  switch (method) {
    case "read":    resp = model.id ? store.find(model) : store.findAll(); break;
  }

  if (resp) {
    options.success(resp);
  } else {
    options.error("Record not found");
  }
};
