Remove use of Ember.on

no issue
- removes the few uses of `Ember.on` for lifecycle hooks or event hooks where order may be important

`Ember.on` use is discouraged so although we haven't used it often I felt like we should ensure we're consistent throughout the codebase. There's a great article with details of why it's discouraged here: http://notmessenger.com/proper-use-of-ember-on/
This commit is contained in:
Kevin Ansfield 2015-12-15 11:09:34 +00:00
parent 29652eb6a4
commit b0e357138b
4 changed files with 50 additions and 37 deletions

View File

@ -1,6 +1,6 @@
import Ember from 'ember'; import Ember from 'ember';
const {Component, on, run} = Ember; const {Component, run} = Ember;
export default Component.extend({ export default Component.extend({
tagName: 'li', tagName: 'li',
@ -8,9 +8,9 @@ export default Component.extend({
active: false, active: false,
linkClasses: null, linkClasses: null,
unfocusLink: on('click', function () { click() {
this.$('a').blur(); this.$('a').blur();
}), },
actions: { actions: {
setActive(value) { setActive(value) {

View File

@ -2,7 +2,7 @@
import Ember from 'ember'; import Ember from 'ember';
import EmberSelectizeComponent from 'ember-cli-selectize/components/ember-selectize'; import EmberSelectizeComponent from 'ember-cli-selectize/components/ember-selectize';
const {computed, isArray, isBlank, get, on, run} = Ember; const {computed, isArray, isBlank, get, run} = Ember;
const emberA = Ember.A; const emberA = Ember.A;
export default EmberSelectizeComponent.extend({ export default EmberSelectizeComponent.extend({
@ -15,28 +15,6 @@ export default EmberSelectizeComponent.extend({
return options; return options;
}), }),
_dontOpenWhenBlank: on('didInsertElement', function () {
let openOnFocus = this.get('openOnFocus');
if (!openOnFocus) {
run.schedule('afterRender', this, function () {
let selectize = this._selectize;
if (selectize) {
selectize.on('dropdown_open', function () {
if (isBlank(selectize.$control_input.val())) {
selectize.close();
}
});
selectize.on('type', function (filter) {
if (isBlank(filter)) {
selectize.close();
}
});
}
});
}
}),
/** /**
* Event callback that is triggered when user creates a tag * Event callback that is triggered when user creates a tag
* - modified to pass the caret position to the action * - modified to pass the caret position to the action
@ -105,9 +83,7 @@ export default EmberSelectizeComponent.extend({
// we have a re-order, update the selection // we have a re-order, update the selection
args.forEach((value) => { args.forEach((value) => {
let obj = selection.find(function (item) { let obj = selection.find(function (item) {
// jscs:disable return `${get(item, valuePath)}` === value;
return (get(item, valuePath) + '') === value;
// jscs:enable
}); });
if (obj) { if (obj) {
@ -116,6 +92,33 @@ export default EmberSelectizeComponent.extend({
}); });
this.set('selection', reorderedSelection); this.set('selection', reorderedSelection);
},
_preventOpeningWhenBlank() {
let openOnFocus = this.get('openOnFocus');
if (!openOnFocus) {
run.schedule('afterRender', this, function () {
let selectize = this._selectize;
if (selectize) {
selectize.on('dropdown_open', function () {
if (isBlank(selectize.$control_input.val())) {
selectize.close();
}
});
selectize.on('type', function (filter) {
if (isBlank(filter)) {
selectize.close();
}
});
}
});
}
},
didInsertElement() {
this._super(...arguments);
this._preventOpeningWhenBlank();
} }
}); });

View File

@ -1,7 +1,7 @@
/*jshint scripturl:true*/ /*jshint scripturl:true*/
import Ember from 'ember'; import Ember from 'ember';
const {$, Component, on} = Ember; const {$, Component} = Ember;
export default Component.extend({ export default Component.extend({
tagName: 'a', tagName: 'a',
@ -15,7 +15,7 @@ export default Component.extend({
// anchor behaviors or ignored // anchor behaviors or ignored
href: Ember.String.htmlSafe('javascript:;'), href: Ember.String.htmlSafe('javascript:;'),
scrollTo: on('click', function () { click() {
let anchor = this.get('anchor'); let anchor = this.get('anchor');
let $el = Ember.$(anchor); let $el = Ember.$(anchor);
@ -32,5 +32,5 @@ export default Component.extend({
$(this).removeAttr('tabindex'); $(this).removeAttr('tabindex');
}).focus(); }).focus();
} }
}) }
}); });

View File

@ -1,7 +1,7 @@
/*global device*/ /*global device*/
import Ember from 'ember'; import Ember from 'ember';
const {TextField, computed, on} = Ember; const {TextField, computed} = Ember;
export default TextField.extend({ export default TextField.extend({
focus: true, focus: true,
@ -16,16 +16,26 @@ export default TextField.extend({
return false; return false;
}), }),
focusField: on('didInsertElement', function () { _focusField() {
// This fix is required until Mobile Safari has reliable // This fix is required until Mobile Safari has reliable
// autofocus, select() or focus() support // autofocus, select() or focus() support
if (this.get('focus') && !device.ios()) { if (this.get('focus') && !device.ios()) {
this.$().val(this.$().val()).focus(); this.$().val(this.$().val()).focus();
} }
}), },
trimValue: on('focusOut', function () { _trimValue() {
let text = this.$().val(); let text = this.$().val();
this.$().val(text.trim()); this.$().val(text.trim());
}) },
didInsertElement() {
this._super(...arguments);
this._focusField();
},
focusOut() {
this._super(...arguments);
this._trimValue();
}
}); });