Skip to content

Instantly share code, notes, and snippets.

@H1D
Forked from rwjblue/components.one-way-input.js
Created October 28, 2016 14:42
Show Gist options
  • Save H1D/87d7fa0bda50e2687df47536137307da to your computer and use it in GitHub Desktop.
Save H1D/87d7fa0bda50e2687df47536137307da to your computer and use it in GitHub Desktop.
One Way Input
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'input',
attributeBindings: [ 'type', 'value', 'placeholder', 'data-stripe', 'name' ],
type: 'text',
_sanitizedValue: undefined,
input() { this._handleChangeEvent(); },
change() { this._handleChangeEvent(); },
_handleChangeEvent() {
let value = this.readDOMAttr('value');
this._processNewValue.call(this, value);
},
_processNewValue(rawValue) {
let value = this.sanitizeInput(rawValue);
if (this._sanitizedValue !== value) {
this._sanitizedValue = value;
this.attrs.update(value);
}
},
sanitizeInput: function(input) {
return input;
},
didReceiveAttrs: function() {
if (!this.attrs.update) {
throw new Error(`You must provide an \`update\` action to \`{{${this.templateName}}}\`.`);
}
this._processNewValue.call(this, this.get('value'));
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
emitCreate(a) {alert(a)},
onEscape() {alert(1)}
}
});
<h1>Welcome!</h1>
{{title}}
{{one-way-input title
class="st-input st-size-normal new-unit__lesson-title"
keyEvents=(hash 13=(action 'emitCreate'))
update=(action (mut title))}}
{
"version": "0.4.8",
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "1.13.13",
"ember-template-compiler": "1.13.13",
"ember-data": "2.9.0"
},
"addons": {
"ember-hash-helper-polyfill": "0.1.1",
"ember-one-way-controls": "1.1.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment