Created
June 11, 2015 13:55
-
-
Save mupkoo/e3ff52839bb9b0e2b28b to your computer and use it in GitHub Desktop.
Ember Simple Form
This file contains 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
# ... | |
def create | |
question = Question.new(question_params) | |
if question.save | |
render json: question, status: 201 | |
else | |
render json: { errors: question.errors }, status: 422 | |
end | |
end | |
# ... |
This file contains 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.Component.extend({ | |
tagName: 'form', | |
submit: function (e) { | |
e.preventDefault(); | |
this.sendAction('action'); | |
}, | |
focusFirstInput: function () { | |
this.$('input').eq(0).focus(); | |
}.on('didInsertElement') | |
}); |
This file contains 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 humanize from 'dashboard/utils/humanize'; | |
export default Ember.Component.extend({ | |
classNameBindings: [ 'resolveClass', 'hasError' ], | |
isCheckbox: Ember.computed.equal('type', 'checkbox'), | |
isTextarea: Ember.computed.equal('type', 'textarea'), | |
initialize: function () { | |
var key = 'parentView.model.errors.' + this.get('value'); | |
Ember.defineProperty(this, 'hasError', Ember.computed(key, function () { | |
var errors = this.get('parentView.model.errors'); | |
return errors && errors.has(this.get('value')); | |
})); | |
Ember.defineProperty(this, 'errors', Ember.computed(key, function () { | |
if (this.get('hasError')) { | |
return this.get(key).map(function (error) { | |
return error.message; | |
}).join(', '); | |
} | |
})); | |
Ember.defineProperty(this, 'resolveValue', Ember.computed('parentView.model.' + this.get(key), function (getKey, value) { | |
var key = 'parentView.model.' + this.get('value'); | |
if (arguments.length > 1) { | |
this.set(key, value); | |
} | |
return this.get(key); | |
})); | |
}.on('init'), | |
resolveClass: function () { | |
return this.get('type') === 'checkbox' ? 'checkbox' : 'form-group'; | |
}.property(), | |
resolveLabel: function () { | |
return this.get('label') || humanize(this.get('value')); | |
}.property('label', 'value'), | |
errorClass: function () { | |
var type = this.get('type'); | |
return type && type.match(/^(text|email|textarea|number|password)$/) ? 'append-error' : 'help-block'; | |
}.property() | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment