var backbone = require('backbone');
var $ = require('jquery');
var _ = require('underscore');
var deep = require('deep-diff');


module.exports = {
  ListView: backbone.BaseListView.extend({
    events: {
      'change': function() {
        var
          schema = this.getSelectedSchema();

        $.getJSON(schema, (function(schemaData) {
          var
            // Keys of entered fields
            keys = _.keys(this.parent.getFilledValues());

          // If data can be lost - show confirmation message
          if(deep.diff(_.pick(schemaData.properties, keys), _.pick(this.parent.layout.form.schema.properties, keys)))
            return window.APP.layout.confirmationDialog
              .setMessage('Some data you have entered will be lost. Are you sure you want to change the Data Package Profile?')

              .setCallbacks({
                yes: (function() {
                  this.parent.reset(schemaData);
                  window.APP.layout.confirmationDialog.deactivate();
                  return false;
                }).bind(this)
              })

              .activate();

          this.parent.reset(schemaData);

        }).bind(this));
      }
    },

    getSelectedSchema: function() {
      return this.collection.get( this.$el.val() ).get('schema');
    },

    ItemView: backbone.BaseView.extend({
      render: function() {
        this.$el
          .attr('value', this.model.cid)
          .html(this.model.get('title'));

        return this;
      },

      tagName: 'option'
    }),

    reset: function() {
      backbone.BaseListView.prototype.reset.apply(this, arguments);

      $.getJSON(this.getSelectedSchema(), (function(schemaData) {
        this.parent.reset(schemaData);
      }).bind(this));

      return this;
    },

    tagName: 'select'
  })
};

@@ -898,39 +898,313 @@
    this.
-parent.reset(schemaData
+selectedValue = this.$el.val();%0A                  this.parent.reset(schemaData);%0A                  window.APP.layout.confirmationDialog.deactivate();%0A                  return false;%0A                %7D).bind(this),%0A%0A                no: (function() %7B%0A                  this.$el.val(this.selectedValue
 );%0A     
@@ -1371,24 +1371,77 @@
 ctivate();%0A%0A
+          else this.selectedValue = this.$el.val();%0A%0A
           th
@@ -1574,16 +1574,25 @@
 llection
+%0A        
 .get( th
@@ -1605,16 +1605,25 @@
 .val() )
+%0A        
 .get('sc
@@ -1891,24 +1891,24 @@
 unction() %7B%0A
-
       backbo
@@ -1962,16 +1962,59 @@
 uments);
+%0A      this.selectedValue = this.$el.val();
 %0A%0A      

Fix no answer confirmation dialog (#5)