nullnullnull
nullnullnull

Reputation: 8189

Getting form values in a view in ember.js

I have the following view:

Whistlr.RegistrationView = Ember.View.extend
  templateName: "users/registration_form"

  submit: (event, view) ->
    event.preventDefault()
    event.stopPropagation()
    $.ajax
      url: '/users'
      type: "POST"
      data:
        "user[username]": @.get 'username'
        "user[email]": @.get 'email'
        "user[password]": @.get 'password'
        "user[password_confirmation]": @.get 'password_confirmation'
      success: (data) ->
        alert "Success!"
      error: (jqXHR, textStatus, errorThrown) ->
        alert jqXHR.responseText

With this template:

form
  label Username
  = input value=username type="text"
  label Email
  = input value=email type="text"
  label Password
  = input value=password type="password"
  label Password Confirmtation
  = input value=password_confirmation type="password"

  = input type='checkbox' checked=view.remember
  label Remember me

  = input type="submit" class="btn"

Unfortunately, the form values are not getting bound to the view, and so the data is being sent as an array of blank fields. It looks like I'm not able to access the form values with a simple @.get like I could with a form that's in a standard template. How can I access these values from within a view?

Upvotes: 0

Views: 678

Answers (1)

nullnullnull
nullnullnull

Reputation: 8189

Looks like the problem was in my template. I needed to specify in each attribute's value field that it was bound to the view. The revised template looks like this:

form
  label Username
  = input value=view.username type="text"
  label Email
  = input value=view.email type="text"
  label Password
  = input value=view.password type="password"
  label Password Confirmtation
  = input value=view.password_confirmation type="password"

  = input type='checkbox' checked=view.remember
  label Remember me

  = input type="submit" class="btn"

Upvotes: 2

Related Questions