GTDev
GTDev

Reputation: 5528

emberjs rails submitting in controller without model

I'm am brand new to emberjs (1.0.0-RC1) which I'm using on top of rails. I want to submit a form for a session without using an ember model. I feel this is better because there is not real session model in my rails app. For now what works is the following:

#login_controller.js.coffee
SkillUp.LoginController = Ember.ObjectController.extend
  # Just a title to pass to the template, for fun
  title: "Login Controller"

  submit: (controller) ->
    model = @get 'model'
    model.get('transaction').commit()

#session.js.coffee
SkillUp.Session = DS.Model.extend
  email: DS.attr 'string'
  password: DS.attr 'string'

#login_route.js.coffee
SkillUp.LoginRoute = Ember.Route.extend
  setupController: (controller) ->
    controller.set 'title', "Login"

    controller.set 'content', SkillUp.Session.createRecord()

<!-- login.handlebars -->
<h2>template: {{title}}</h2>
<form>
  {{#with controller}}
    email: {{view Ember.TextField valueBinding="email"}}
    password: {{view Ember.TextField valueBinding="password" type="password"}}
    <button {{action submit}}>Submit</button>
  {{/with}}
</form>

As I said my goal is to remove the session.js.coffee because a rails model does not exist for it.

Help would be appreciated thanks

Upvotes: 0

Views: 130

Answers (2)

ahawkins
ahawkins

Reputation: 1174

I don't recommend implementing $.ajax directly. I recommend keeping the DS model. This should make testing easier if you use DS.FixtureAdapter. Ideally you should keep all the ajax behind a layer in the application. You shouldn't be dropping in $.ajax in various parts of the application.

EDIT: just because you don't have a Session model in the backend doesn't mean you can't have one on the frontend. The ember app will have it's own models. Don't worry about a 1:1 mapping.

Upvotes: 1

Christopher Swasey
Christopher Swasey

Reputation: 10552

You can just use $.ajax in your controller method to post the values to rails using jQuery manually.

$.ajax('/sessions', {
    type: 'POST',
    data: {
        email: this.get('email'),
        password: this.get('password)
    },
    ...
});

Add appropriate handlers for success and failure.

Upvotes: 0

Related Questions