/*!
    Copyright (C) 2016 Google Inc., authors, and contributors <see AUTHORS file>
    Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
    Created By: ivan@reciprocitylabs.com
    Maintained By: ivan@reciprocitylabs.com
*/

(function (can, $) {
  'use strict';

  GGRC.Components('inline-edit', {
    tag: 'inline-edit',
    template: can.view(
      GGRC.mustache_path +
      '/components/inline_edit/inline.mustache'
    ),
    scope: {
      instance: null,
      type: '@',
      caId: null,
      property: null,
      values: null,
      isSaving: false,
      context: {
        isEdit: false,
        value: null,
        values: null,
        title: null,
        label: null
      },
      enableEdit: function (ctx, el, ev) {
        ev.preventDefault();

        this.attr('context.isEdit', true);
      },
      onCancel: function (ctx, el, ev) {
        ev.preventDefault();
        this.attr('context.isEdit', false);
        this.attr('context.value', this.attr('property'));
      },
      onSave: function (ctx, el, ev) {
        var caid = this.attr('caId');
        var prop = this.attr('property');
        var instance = this.attr('instance');
        var oldValue = this.attr('property');
        var value = this.attr('context.value');

        ev.preventDefault();
        this.attr('context.isEdit', false);

        if (oldValue === value) {
          return;
        }

        if (this.attr('caId')) {
          if (this.attr('type') === 'checkbox') {
            value = value ? 1 : 0;
          }
          instance.attr('custom_attributes.' + caid, value);
        } else {
          instance.attr(prop, value);
        }

        this.attr('isSaving', true);
        instance.save().then(function () {
          this.attr('isSaving', false);
        }.bind(this));
      }
    },
    init: function () {
      var scope = this.scope;
      var value = scope.attr('property');

      if (!scope.instance.custom_attributes) {
        _.defer(scope.instance.setup_custom_attributes.bind(scope.instance));
      }

      if (scope.attr('caId') && scope.attr('type') === 'checkbox') {
        value = value === '1';
      }
      scope.attr('context.value', value);
      scope.attr('context.title', scope.attr('title'));
      scope.attr('context.label', scope.attr('label'));
      if (_.isString(scope.attr('values'))) {
        scope.attr('context.values', scope.attr('values').split(','));
      }
    },
    events: {
      click: function () {
        this.scope.attr('isInside', true);
      },
      '{window} click': function (el, ev) {
        if (this.scope.attr('context.isEdit') &&
            !this.scope.attr('isInside')) {
          this.scope.onSave(this.scope, el, ev);
        }
        this.scope.attr('isInside', false);
      }
    }
  });
})(window.can, window.can.$);

@@ -1415,32 +1415,117 @@
 urn;%0A        %7D%0A%0A
+        this.attr('isSaving', true);%0A        instance.refresh().then(function () %7B%0A  
         if (this
@@ -1537,24 +1537,26 @@
 ('caId')) %7B%0A
+  
           if
@@ -1597,32 +1597,34 @@
 ) %7B%0A            
+  
 value = value ? 
@@ -1630,16 +1630,18 @@
  1 : 0;%0A
+  
         
@@ -1646,32 +1646,34 @@
     %7D%0A          
+  
 instance.attr('c
@@ -1715,16 +1715,18 @@
         
+  
 %7D else %7B
@@ -1728,32 +1728,34 @@
 lse %7B%0A          
+ 
+ 
 instance.attr(pr
@@ -1778,48 +1778,15 @@
     
+  
 %7D%0A%0A  
-      this.attr('isSaving', true);%0A
     
@@ -1826,32 +1826,34 @@
  () %7B%0A          
+  
 this.attr('isSav
@@ -1858,32 +1858,57 @@
 aving', false);%0A
+          %7D.bind(this));%0A
         %7D.bind(t

Refresh instance before save