Ember Data Mockjax

HTML

<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.5.2/jquery.mockjax.js"></script>
<script src="http://ember.alexspeller.com/handlebars-1.0.0.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script src="http://ember.alexspeller.com/canary/shas/5c30be4fe354b57a9241be6cd14f0b3fae977c0e/ember.js"></script>
<script src="http://ember.alexspeller.com/canary/shas/45d196b2907ae0ddca2ffc2b50ce84fbc0e275c6/ember-data.js"></script>
<script type="text/x-handlebars">
  <div class="container">
    <h2>Create User</h2>

    {{error-messages for=this}}

    {{#object-form for=this action="updateUser" buttonLabel="Save"}}
      {{form-field for="name"}}
      {{form-field for="email"}}
    {{/object-form}}

  </div>
</script>

<script type="text/x-handlebars" data-template-name="components/error-messages">
  {{#if errors.length}}
    <div class="alert alert-danger">
      <h4>There was a problem</h4>
      <ul>
        {{#each baseErrors}}
          <li>
            {{message}}
          </li>
        {{/each}}

        {{#each errors}}
          <li>
            {{titleize attribute}} {{message}}
          </li>
        {{/each}}
      </ul>
    </div>
  {{/if}}
</script>


<script type="text/x-handlebars" data-template-name="components/form-field">
  <div class="form-group" {{bind-attr class="hasError:has-error"}}>
    <label {{bind-attr for=label}}>{{label}}</label>
    {{#if template}}
      {{yield}}
    {{else}}
      {{input type=type value=value id=label class="form-control"}}
    {{/if}}
      
  {{#if errors.length}}
    <span class="help-block">
      {{errors}}
    </span>
  {{/if}}

  </div>
</script>

<script type="text/x-handlebars" data-template-name="components/object-form">
  <form {{action "submit" on="submit"}}>

    {{yield}}

    <div class="form-group">
      <button type='submit' class='btn btn-block btn-primary'>{{buttonLabel}}</button>
    </div>
  </form>
</script>

CoffeeScript

window.App = Em.Application.create()

App.ApplicationAdapter = DS.RESTAdapter.extend
  ajaxError: (jqXHR) ->
    error = @_super(jqXHR)

    if jqXHR and jqXHR.status is 422
      response = Ember.$.parseJSON(jqXHR.responseText)
      errors = {}
      if response.errors?
        jsonErrors = response.errors
        Ember.keys(jsonErrors).forEach (key) ->
          errors[Ember.String.camelize(key)] = jsonErrors[key]

      new DS.InvalidError(errors)
    else
      error

App.ApplicationRoute = Em.Route.extend
  model: ->
    @store.createRecord 'user'

  actions:
    updateUser: ->
      @currentModel.save().then ->
        alert 'User created'
      , ->
        # You have to pass an error handler to the save promise,
        # even if it does nothing, otherwise an exception will
        # be thrown and the errors won't be populated for you
            


App.User = DS.Model.extend
  name:   DS.attr 'string'
  email:  DS.attr 'string'


$.mockjax
  url: "/users"
  type: 'POST'
  status: 422
  responseText:
    errors:
      name:      ["must be present"]
      email:     ["is not an email", "is blank"]
      the_force: ["is not strong in this one"]
      base:      ["This person is a generally unsavoury character and is not allowed to sign up"]

DS.Model.reopen
  adapterDidInvalidate: (errors) ->
    recordErrors = @get 'errors'
    for own key, errorValue of errors
      recordErrors.add key, errorValue

        
Ember.Handlebars.helper 'titleize', (text) ->
  text.titleize()

String::titleize = ->
  @underscore().replace(/_/g, " ").capitalize()


App.ErrorMessagesComponent = Em.Component.extend
  errors: Em.computed.filter 'for.errors.content', (error) ->
    error.attribute != 'base'

  baseErrors: Em.computed.filter 'for.errors.content', (error) ->
    error.attribute is 'base'
        
# For a detailed rundown of the object form components below, see:
# http://alexspeller.com/simple-forms-with-ember/

App.ObjectFormComponent = Em.Component.extend
 ...