Skip to content

Instantly share code, notes, and snippets.

@mehulkar
Last active December 17, 2015 15:18
Show Gist options
  • Save mehulkar/5630313 to your computer and use it in GitHub Desktop.
Save mehulkar/5630313 to your computer and use it in GitHub Desktop.
App.ServerValidatedTextField = Ember.View.extend
tagName: 'input'
type: 'text'
###
This is handy if you want your server to validate an input field.
Your server is expected to return a boolean value
When that `validInput` attribute changes, we'll replace the layoutTemplate
with something that indicates valid or invalid.
You'll need three templates: `_loadingTemplate`, `_validTemplate`, `_invalidTemplate`
###
# specify a url and we'll make a GET request that expects a bool response
# alternatively, you can overwrite the `validateInputValue` function
# and instantiate this view with it
url: null
# the textfield is not validate to begin with
validInput: false
focusOut: -> @validateInputValue()
validateInputValue: ->
# if no url is defined, we won't do anything
return if !@get('url')?
# interim layout
@set('layoutName', '_loadingTemplate')
$.get url, (bool) =>
if typeof bool is "boolean"
@set('validInput', bool)
else
Ember.assert('The server did not return a boolean value')
changeLayout: (->
templateName = if @get('validInput') == true then '_validTemplate' else '_invalidTemplate'
@set('layoutName', templateName)
).observes('validInput')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment