Skip to content

Instantly share code, notes, and snippets.

@trmcnvn
Created June 16, 2017 05:39
Show Gist options
  • Save trmcnvn/88999a2a9313b8b2ef26e4bc35191026 to your computer and use it in GitHub Desktop.
Save trmcnvn/88999a2a9313b8b2ef26e4bc35191026 to your computer and use it in GitHub Desktop.
Changeset Example
import Ember from 'ember';
import Changeset from 'ember-changeset';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
init() {
this._super(...arguments);
this.model = new Ember.Object({ inputValue: 'Hello, World!' });
this.changeset = new Changeset(this.model, this.validate);
},
validate({ newValue }) {
return newValue.length > 0 && newValue.length <= 140;
},
actions: {
makeChanges() {
this.changeset.execute();
}
}
});
<h1>Changeset</h1>
<input value={{changeset.inputValue}} oninput={{action (mut changeset.inputValue) value="target.value"}}>
<br><br><br>
<p>Changeset Valid: {{changeset.isValid}}</p>
<p>Model Value: {{model.inputValue}}</p>
<button onclick={{action "makeChanges"}}>Save Changes</button>
{
"version": "0.12.1",
"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.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1",
"ember-cp-validations": "3.3.2",
"ember-changeset": "1.3.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment