Skip to content

Instantly share code, notes, and snippets.

@shoxter
Created June 22, 2016 15:56
Show Gist options
  • Save shoxter/8eda979f6c533e23e2fc4abdd10cca37 to your computer and use it in GitHub Desktop.
Save shoxter/8eda979f6c533e23e2fc4abdd10cca37 to your computer and use it in GitHub Desktop.
Bug
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
oldLength: 0,
tempValue: '',
actions: {
changeValue: function(e) {
var input = e.target.value;
input = input.replace(/[^\d\(\) -]/g, '');
input = input.substr(0, 14);
if (input.length < this.get('oldLength')) {
switch (input.length) {
case 0:
this.set('tempValue', '');
break;
case 1:
this.set('tempValue', '');
break;
case 6:
this.set('tempValue', input.substr(0,4));
break;
case 10:
this.set('tempValue', input.substr(0,9));
break;
default:
this.set('tempValue', input);
}
} else {
switch (input.length) {
case 0:
this.set('tempValue', '');
break;
case 1:
this.set('tempValue', '(' + input);
break;
case 4: this.set('tempValue', input + ') ');
break;
case 9:
this.set('tempValue', input + '-');
break;
case 15:
this.set('tempValue', this.get('tempValue'));
break;
default:
this.set('tempValue', input);
}
}
this.set('oldLength', this.get('tempValue.length'));
}
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
<input value={{tempValue}} oninput={{action "changeValue"}}>
{{tempValue}}
<br>
<br>
{
"version": "0.9.3",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.6.0",
"ember-data": "2.6.1",
"ember-template-compiler": "2.6.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment