define(
  [
    'underscore',
    'dispatchers/Dispatcher',
    'stores/Store',
    'rsvp',
    'constants/ProjectInstanceConstants',
    'controllers/NotificationController',
    'models/Instance',
    'backbone',
    'globals'
  ],
  function (_, Dispatcher, Store, RSVP, ProjectInstanceConstants, NotificationController, Instance, Backbone, globals) {

    var _projectInstances = {};
    var _isBeingFetched = {};

    //
    // Project Instance Model
    // Mainly a helper for generating add/remove urls
    //

    var ProjectInstance = Backbone.Model.extend({
      urlRoot: function() {
        return globals.API_ROOT + "/project/" + this.project.id + "/instance";
      },

      url: function () {
        var url = Backbone.Model.prototype.url.apply(this) + globals.slash();
        return url;
      },

      initialize: function(options){
        this.instance = options.instance;
        this.project = options.project;
        this.set("id", this.instance.id);
      }
    });

    var ProjectInstanceCollection = Backbone.Collection.extend({
      model: Instance,

      url: function () {
        var url = globals.API_ROOT + "/project/" + this.project.id + "/instance" + globals.slash();
        return url;
      },

      initialize: function(models, options){
        this.project = options.project;
      }
    });

    //
    // CRUD Operations
    //

    var fetchProjectInstances = function (project) {
      _isBeingFetched[project.id] = true;
      var promise = new RSVP.Promise(function (resolve, reject) {
        var projectInstances = new ProjectInstanceCollection(null, {
          project: project
        });
        projectInstances.fetch().done(function () {
          _isBeingFetched[project.id] = false;
          _projectInstances[project.id] = projectInstances;
          resolve();
        });
      });
      return promise;
    };

    function addInstanceToProject(instance, project){
      var projectInstance = new ProjectInstance({
        instance: instance,
        project: project
      });

      projectInstance.save().done(function(){
        var successMessage = "Instance '" + instance.get('name') + "' added to Project '" + project.get('name') + "'";
        //NotificationController.success(successMessage);
      }).fail(function(){
        var failureMessage = "Error adding Instance '" + instance.get('name') + "' to Project '" + project.get('name') + "'.";
        NotificationController.error(failureMessage);
        _projectInstances[project.id].remove(instance);
      });
      _projectInstances[project.id].add(instance);
    }

    function removeInstanceFromProject(instance, project){
      var projectInstance = new ProjectInstance({
        instance: instance,
        project: project
      });

      projectInstance.destroy().done(function(){
        var successMessage = "Instance '" + instance.get('name') + "' removed from Project '" + project.get('name') + "'";
        //NotificationController.success(successMessage);
      }).fail(function(){
        var failureMessage = "Error removing Instance '" + instance.get('name') + "' from Project '" + project.get('name') + "'.";
        NotificationController.error(failureMessage);
        _projectInstances[project.id].add(instance);
      });
      _projectInstances[project.id].remove(instance);
    }

    //
    // Project Instance Store
    //

    var ProjectInstanceStore = {

//      getInstancesInProject: function (project) {
//        var projectInstances = _projectInstances[project.id];
//        var instancesAreBeingFetched = _isBeingFetched[project.id];
//
//        // If there are no instances for the project, and the instances aren't being fetched
//        // already, then go fetch them, otherwise return the instances we already have
//        if(!projectInstances && !instancesAreBeingFetched) {
//          fetchProjectInstances(project).then(function(){
//            ProjectInstanceStore.emitChange();
//          }.bind(this));
//        }
//        return projectInstances;
//      }

    };

    Dispatcher.register(function (payload) {
      var action = payload.action;

      switch (action.actionType) {
        case ProjectInstanceConstants.ADD_INSTANCE_TO_PROJECT:
          addInstanceToProject(action.instance, action.project);
          break;

        case ProjectInstanceConstants.REMOVE_INSTANCE_FROM_PROJECT:
          removeInstanceFromProject(action.instance, action.project);
          break;

        default:
          return true;
      }

      ProjectInstanceStore.emitChange();

      return true;
    });

    _.extend(ProjectInstanceStore, Store);

    return ProjectInstanceStore;
  });

@@ -430,1454 +430,30 @@
  // 
-Project Instance Model%0A    // Mainly a helper for generating add/remove urls%0A    //%0A%0A    var ProjectInstance = Backbone.Model.extend(%7B%0A      urlRoot: function() %7B%0A        return globals.API_ROOT + %22/project/%22 + this.project.id + %22/instance%22;%0A      %7D,%0A%0A      url: function () %7B%0A        var url = Backbone.Model.prototype.url.apply(this) + globals.slash();%0A        return url;%0A      %7D,%0A%0A      initialize: function(options)%7B%0A        this.instance = options.instance;%0A        this.project = options.project;%0A        this.set(%22id%22, this.instance.id);%0A      %7D%0A    %7D);%0A%0A    var ProjectInstanceCollection = Backbone.Collection.extend(%7B%0A      model: Instance,%0A%0A      url: function () %7B%0A        var url = globals.API_ROOT + %22/project/%22 + this.project.id + %22/instance%22 + globals.slash();%0A        return url;%0A      %7D,%0A%0A      initialize: function(models, options)%7B%0A        this.project = options.project;%0A      %7D%0A    %7D);%0A%0A    //%0A    // CRUD Operations%0A    //%0A%0A    var fetchProjectInstances = function (project) %7B%0A      _isBeingFetched%5Bproject.id%5D = true;%0A      var promise = new RSVP.Promise(function (resolve, reject) %7B%0A        var projectInstances = new ProjectInstanceCollection(null, %7B%0A          project: project%0A        %7D);%0A        projectInstances.fetch().done(function () %7B%0A          _isBeingFetched%5Bproject.id%5D = false;%0A          _projectInstances%5Bproject.id%5D = projectInstances;%0A          resolve();%0A        %7D);%0A      %7D);%0A      return promise;%0A    %7D;
+CRUD Operations%0A    //
 %0A%0A  
@@ -1993,638 +1993,8 @@
  %7B%0A%0A
-//      getInstancesInProject: function (project) %7B%0A//        var projectInstances = _projectInstances%5Bproject.id%5D;%0A//        var instancesAreBeingFetched = _isBeingFetched%5Bproject.id%5D;%0A//%0A//        // If there are no instances for the project, and the instances aren't being fetched%0A//        // already, then go fetch them, otherwise return the instances we already have%0A//        if(!projectInstances && !instancesAreBeingFetched) %7B%0A//          fetchProjectInstances(project).then(function()%7B%0A//            ProjectInstanceStore.emitChange();%0A//          %7D.bind(this));%0A//        %7D%0A//        return projectInstances;%0A//      %7D%0A%0A
     

remove unused functions