Skip to content

Instantly share code, notes, and snippets.

@spencer516
Last active November 20, 2015 05:12
Show Gist options
  • Save spencer516/48a64fc6f767eebe9f2a to your computer and use it in GitHub Desktop.
Save spencer516/48a64fc6f767eebe9f2a to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Ember Twiddle'
});
<h1>Parent</h1>
{{#my-parent as |notifyChange|}}
<b>There must be a 't' in the inputs to be valid</b><br>
{{my-child notify=(action notifyChange 'path-a')}}<br>
{{my-child notify=(action notifyChange 'path-b')}}
{{/my-parent}}
import Ember from 'ember';
export default Ember.TextField.extend({
keyUp() {
var value = this.get('value');
var notify = this.getAttr('notify');
var isValid = this.validate(value);
debugger
notify(value, isValid);
},
validate(value) {
return value.indexOf('t') > -1;
}
});
import Ember from 'ember';
export default Ember.Component.extend({
buffer: null,
bufferJSON: '',
notify(path, value, isValid) {
var buffer = this.get('buffer');
this.set('buffer.'+path, {isValid, value});
this.set('bufferJSON', JSON.stringify(buffer));
this.set('notValid', !this.checkIsValid(buffer));
},
checkIsValid(buffer) {
return Object.keys(buffer).every(key => {
return buffer[key].isValid;
});
},
flushBuffer() {
var buffer = this.get('buffer');
this.set('buffer', {});
Object.keys(buffer).forEach(key => {
// Do something here to add/update this
// item in the array.
});
},
notValid: true,
init() {
this._super();
this.set('buffer', {});
},
actions: {
save() {
this.flushBuffer();
}
}
});
{{yield (action notify)}}
<br>
{{bufferJSON}}
<br>
<button disabled={{notValid}}>Save</button>
{
"version": "0.4.16",
"EmberENV": {
"FEATURES": {}
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.1.0/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.1.0/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.1.0/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment