Created
March 1, 2017 22:51
-
-
Save SuperCoolFrog/c5cec3c7f2a78502cdc260a23e908911 to your computer and use it in GitHub Desktop.
Async Validator with Injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import Changeset from 'ember-changeset'; | |
import lookupValidator from 'ember-changeset-validations'; | |
import ValidationFactory from 'twiddle/validations/application-factory'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle', | |
store: Ember.inject.service('store'), | |
init() { | |
let changeset, validations; | |
this._super(); | |
validations = ValidationFactory.create(this.get('store')); | |
changeset = new Changeset({}, lookupValidator(validations), validations); | |
changeset.validate(); | |
this.set('changeset', changeset); | |
}, | |
error: Ember.computed('changeset.error', function() { | |
return JSON.stringify(this.get('changeset.error'), null, 2); | |
}), | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Service.extend({ | |
isOneAsync(v) { | |
return new Ember.RSVP.Promise((resolve) => { | |
Ember.run.later(this, () => { | |
resolve(v == 1); | |
}, 2000); | |
}); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.11.0", | |
"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.10.2", | |
"ember-data": "2.11.0", | |
"ember-template-compiler": "2.10.2", | |
"ember-testing": "2.10.2" | |
}, | |
"addons": { | |
"ember-changeset": "*", | |
"ember-changeset-validations": "*" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import validateIsOne from 'twiddle/validators/is-one'; | |
export function create(store) { | |
return { | |
textValue: validateIsOne({ store }) | |
}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default function validateIsOne({ store }) { | |
return (key, newValue, oldValue, changes, content) => { | |
return store.isOneAsync(newValue) | |
.then(isOne => isOne || 'The value entered is not 1'); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment