Last active
August 29, 2015 14:17
-
-
Save koryteg/2e219531c14ce8f0723c to your computer and use it in GitHub Desktop.
valid-form component
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'` | |
ValidFormComponent = Ember.Component.extend( | |
tagName: "form" | |
attributeBindings: ['role'] | |
role: 'form' | |
action: 'submit' | |
submit: (e)-> | |
if e | |
e.preventDefault() | |
validationPromise = @get("model").validate() | |
validationPromise.then( (=> | |
@get("targetObject").send(@get('action')) | |
), (=> | |
console.log @get("model") | |
) | |
) | |
) | |
`export default ValidFormComponent` |
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'` | |
ValidInputContainerComponent = Ember.Component.extend( | |
tagName: 'div' | |
classNameBindings: ['hasSuccess', 'hasWarning', 'hasError', 'hasFeedback'] | |
canShowErrors: false | |
hasFeedback: true | |
# getting the errors on the model. | |
errors: (-> | |
@get('model.errors').get(@get("property")) | |
).property("propVal.length", 'model') | |
# getting the errors on the model. | |
hasError: (-> | |
if @get("canShowErrors") and @get('errors.length') | |
@set("icon", "fa-times") | |
return true | |
else | |
return false | |
).property('canShowErrors', "errors.length") | |
hasSuccess: (-> | |
if @get("canShowErrors") and !@get('errors.length') | |
@set("icon", "fa-check") | |
return true | |
else | |
return false | |
).property('canShowErrors', "errors.length") | |
# setting the form property | |
form: (-> | |
parentView = @get("parentView") | |
while parentView | |
if parentView.get("tagName") == "form" | |
return parentView | |
parentView = parentView.get('parentView') | |
Ember.assert(false, "cannot find form") | |
).property("parentView") | |
# setting the model so we can get the errors on the property. | |
model: (-> | |
@get("form.model") | |
).property("form", "form.model") | |
# hides errors on model change | |
modelChange: (-> | |
@set('canShowErrors', false) | |
).observes("model") | |
# shows errors on un focusing from input | |
focusOut: -> | |
@set('canShowErrors', true) | |
) | |
`export default ValidInputContainerComponent` |
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'` | |
ValidInputComponent = Ember.TextField.extend( | |
attributeBindings: ['placeholder', 'required', 'autofocus', 'disabled', 'value'] | |
placeholder: Em.computed.alias('parentView.placeholder') | |
required: Em.computed.alias('parentView.required') | |
autofocus: Em.computed.alias('parentView.autofocus') | |
disabled: Em.computed.alias('parentView.disabled') | |
type: Em.computed.alias('parentView.type') | |
model: Em.computed.alias('parentView.model') | |
setValue: (-> | |
propName = @get("parentView.property").toString() | |
@get('model').set(propName, @get("value") ) | |
@set('parentView.propVal', @get("value") ) | |
).observes('value.length') | |
modelChange: (-> | |
propName = @get("parentView.property").toString() | |
if @get('model').get(propName) | |
@set('value', @get('model').get(propName) ) | |
else | |
@set('value', "" ) | |
).observes("model") | |
didInsertElement: -> | |
propName = @get("parentView.property").toString() | |
@set('value', @get('model').get(propName) ) | |
) | |
`export default ValidInputComponent` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment