define(
  [
    'backbone',
    'underscore',
    'globals',
    'models/Instance',
    './VolumeState'
  ],
  function (Backbone, _, globals, Instance, VolumeState) {

    return Backbone.Model.extend({
      urlRoot: globals.API_V2_ROOT + "/volumes",

      parse: function (attributes) {
        attributes.start_date = new Date(attributes.start_date);
        attributes.state = new VolumeState({status_raw: attributes.status});
        attributes.status = attributes.status || "Unknown";

        if (attributes.attach_data && attributes.attach_data.instance_alias) {
          attributes.attach_data = {
            device: attributes.attach_data.device,
            instance_id: attributes.attach_data.instance_alias
          };
        }else{
          attributes.attach_data = {
            device: null,
            instance_id: null
          };
        }

        return attributes;
      },

      getCreds: function () {
        return {
          provider_id: this.get('provider'),
          identity_id: this.get('identity').id
        };
      },

      isAttached: function () {
        return this.get('status') == 'in-use' || this.get('status') == 'detaching';
      },

      attachTo: function (instance, mount_location, options) {
        if (!options) options = {};
        if (!options.success) options.success = function () {
        };
        if (!options.error) options.error = function () {
        };

        this.set({
          'status': 'attaching'
        });

        this.get('attach_data').instance_id = instance.get('id');

        var param = {
          volume_id: this.get('id'),
          action: "attach_volume",
          mount_location: mount_location
        };

        var self = this;
        var instanceUrl = instance.url();
        if(instanceUrl.slice(-1) !== "/") instanceUrl += "/";
        var action_url = instanceUrl + 'action' + globals.slash();

        $.ajax({
          url: action_url,
          type: 'POST',
          data: JSON.stringify(param),
          dataType: "json",
          contentType: 'application/json',
          success: function (response_text, textStatus, jqXHR) {
            options.success(response_text);
          },
          error: function (jqXHR, textStatus, errorThrown) {
            self.set({
              'status': 'available'
            });
            self.get('attach_data').instance_id = null;

            options.error(jqXHR.responseJSON);
          }
        });
      },

      getAttachedInstance: function () {
        if (!this.isAttached()) throw "Unattached volume";
        return new Instance({
          id: this.get('attach_data').instance_id,
          identity: this.get('identity')
        });
      },

      detach: function (options) {
        if (!options) options = {};
        if (!options.success) options.success = function () {
        };
        if (!options.error) options.error = function () {
        };

        var param = {
          volume_id: this.get('id'),
          action: "detach_volume"
        };
        var instance = this.getAttachedInstance();

        this.set({'status': 'detaching'});
        var self = this;
        var instanceUrl = instance.url();
        if(instanceUrl.slice(-1) !== "/") instanceUrl += "/";
        var action_url = instanceUrl + 'action' + globals.slash();

        $.ajax({
          url: action_url,
          type: 'POST',
          data: JSON.stringify(param),
          dataType: 'json',
          contentType: 'application/json',
          success: function (response_data) {
            self.set({
              'attach_data': {},
              'status': 'available'
            });
            self.trigger('detach');
            options.success();
          },
          error: function (response_data) {
            options.error('failed to detach volume', response_data);
            self.set({'status': 'in-use'});
          }
        });
      }

    });

  });

@@ -4,17 +4,57 @@
 ine(
-%0A  %5B%0A    
+function (require) %7B%0A%0A    var Backbone = require(
 'bac
@@ -59,22 +59,39 @@
 ackbone'
+)
 ,%0A    
+    _ = require(
 'undersc
@@ -94,22 +94,45 @@
 erscore'
+)
 ,%0A    
+    globals = require(
 'globals
@@ -132,22 +132,46 @@
 globals'
+)
 ,%0A    
+   
+ Instance = require(
 'models/
@@ -183,17 +183,19 @@
 nce'
+)
 ,%0A    
-'./
+    
 Volu
@@ -205,59 +205,22 @@
 tate
-'%0A  %5D,%0A  function (Backbone, _, globals, Instance, 
+ = require('./
 Volu
@@ -226,19 +226,19 @@
 umeState
-) %7B
+');
 %0A%0A    re
@@ -978,3024 +978,116 @@
     
-getCreds: function () %7B%0A        return %7B%0A          provider_id: this.get('provider'),%0A          identity_id: this.get('identity').id%0A        %7D;%0A      %7D,%0A%0A      isAttached: function () %7B%0A        return this.get('status') == 'in-use' %7C%7C this.get('status') == 'detaching';%0A      %7D,%0A%0A      attachTo: function (instance, mount_location, options) %7B%0A        if (!options) options = %7B%7D;%0A        if (!options.success) options.success = function () %7B%0A        %7D;%0A        if (!options.error) options.error = function () %7B%0A        %7D;%0A%0A        this.set(%7B%0A          'status': 'attaching'%0A        %7D);%0A%0A        this.get('attach_data').instance_id = instance.get('id');%0A%0A        var param = %7B%0A          volume_id: this.get('id'),%0A          action: %22attach_volume%22,%0A          mount_location: mount_location%0A        %7D;%0A%0A        var self = this;%0A        var instanceUrl = instance.url();%0A        if(instanceUrl.slice(-1) !== %22/%22) instanceUrl += %22/%22;%0A        var action_url = instanceUrl + 'action' + globals.slash();%0A%0A        $.ajax(%7B%0A          url: action_url,%0A          type: 'POST',%0A          data: JSON.stringify(param),%0A          dataType: %22json%22,%0A          contentType: 'application/json',%0A          success: function (response_text, textStatus, jqXHR) %7B%0A            options.success(response_text);%0A          %7D,%0A          error: function (jqXHR, textStatus, errorThrown) %7B%0A            self.set(%7B%0A              'status': 'available'%0A            %7D);%0A            self.get('attach_data').instance_id = null;%0A%0A            options.error(jqXHR.responseJSON);%0A          %7D%0A        %7D);%0A      %7D,%0A%0A      getAttachedInstance: function () %7B%0A        if (!this.isAttached()) throw %22Unattached volume%22;%0A        return new Instance(%7B%0A          id: this.get('attach_data').instance_id,%0A          identity: this.get('identity')%0A        %7D);%0A      %7D,%0A%0A      detach: function (options) %7B%0A        if (!options) options = %7B%7D;%0A        if (!options.success) options.success = function () %7B%0A        %7D;%0A        if (!options.error) options.error = function () %7B%0A        %7D;%0A%0A        var param = %7B%0A          volume_id: this.get('id'),%0A          action: %22detach_volume%22%0A        %7D;%0A        var instance = this.getAttachedInstance();%0A%0A        this.set(%7B'status': 'detaching'%7D);%0A        var self = this;%0A        var instanceUrl = instance.url();%0A        if(instanceUrl.slice(-1) !== %22/%22) instanceUrl += %22/%22;%0A        var action_url = instanceUrl + 'action' + globals.slash();%0A%0A        $.ajax(%7B%0A          url: action_url,%0A          type: 'POST',%0A          data: JSON.stringify(param),%0A          dataType: 'json',%0A          contentType: 'application/json',%0A          success: function (response_data) %7B%0A            self.set(%7B%0A              'attach_data': %7B%7D,%0A              'status': 'available'%0A            %7D);%0A            self.trigger('detach');%0A            options.success();%0A          %7D,%0A          error: function (response_data) %7B%0A            options.error('failed to detach volume', response_data);%0A            self.set(%7B'status': 'in-use'%7D);%0A          %7D%0A        %7D)
+isAttached: function () %7B%0A        return this.get('status') == 'in-use' %7C%7C this.get('status') == 'detaching'
 ;%0A  

remove unused functions