Skip to content

Commit

Permalink
Fix one-way binding
Browse files Browse the repository at this point in the history
  • Loading branch information
dgaus committed Jul 19, 2016
1 parent 5d006f6 commit 50b7c33
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions addon/components/x-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,68 +91,66 @@ export default Ember.Component.extend({
this._updateValue();
}

this.sendAction('action', this.get('value'), this);
this.sendAction('onchange', this, this.get('value'), event);
this.sendAction('action', this._getValue(), this);
this.sendAction('onchange', this, this._getValue(), event);
},

/**
* When the click DOM event fires on the element, trigger the
* component's action with the component, x-select value, and the jQuery event.
*/
click(event) {
this.sendAction('onclick', this, this.get('value'), event);
this.sendAction('onclick', this, this._getValue(), event);
},

/**
* When the blur DOM event fires on the element, trigger the
* component's action with the component, x-select value, and the jQuery event.
*/
blur(event) {
this.sendAction('onblur', this, this.get('value'), event);
this.sendAction('onblur', this, this._getValue(), event);
},

/**
* When the focusOut DOM event fires on the element, trigger the
* component's action with the component, x-select value, and the jQuery event.
*/
focusOut(event) {
this.sendAction('onfocusout', this, this.get('value'), event);
this.sendAction('onfocusout', this, this._getValue(), event);
},

/**
* Updates `value` with the object associated with the selected option tag
* Returns the object associated with the selected option tag
*
* @private
*/
_updateValueSingle: function(){
_getValueSingle: function(){
var option = this.get('options').find(function(option) {
return option.$().is(':selected');
});

if (option) {
this.set('value', option.get('value'));
return option.get('value');
} else {
this.set('value', null);
return null;
}
},

/**
* Updates `value` with an array of objects associated with the selected option tags
* Returns an array of objects associated with the selected option tags
*
* @private
*/
_updateValueMultiple: function() {
_getValueMultiple: function() {
var options = this.get('options').filter(function(option) {
return option.$().is(':selected');
});

this.set('value', Ember.A(options).mapBy('value'));
return Ember.A(options).mapBy('value');
},

/**
* A utility method to determine if the select is multiple or single and call
* its respective method to update the value.
*
* A utility method to update the value.
* @private
* @utility
*/
Expand All @@ -161,10 +159,19 @@ export default Ember.Component.extend({
return;
}

this.set('value', this._getValue());
},

/**
* Determine if the select is multiple or single and
* get the value for the currently selected options
* @private
*/
_getValue: function() {
if (this.get('multiple')) {
this._updateValueMultiple();
return this._getValueMultiple();
} else {
this._updateValueSingle();
return this._getValueSingle();
}
},

Expand Down

0 comments on commit 50b7c33

Please sign in to comment.