How do you POST a model?

So, I’ve figured out the basics of setting up a model and I can GET things from it

window.App = Ember.Application.create();

App.ApplicationAdapter = DS.RESTAdapter.extend({
    namespace: 'api'
});

App.Router.map(function() {
    this.resource('users');
});

App.UsersRoute = Ember.Route.extend({
    model: function() {
        var users = this.store.find('user');
        return users;
    }
});

var attr = DS.attr;

App.User = DS.Model.extend({
    email: attr(),
    password: attr(),
    zip: attr()
});

This allows me to hit /users, which sends a GET to /api/users, and get JSON with all the users (using Laravel on the back-end). Works great. I’ve tried manually entering some data into the database and I can use it in Ember just fine.

Now how would I go about having a little ‘register’ form where I can put some info in and then POST that back to /api/users?

Either in your route or controller, add an actions hash with something like this:

actions: {
  createUser: function() {
    this.store.createRecord('user', {
      email: 'foo@bar.com',
      password: 'foobar'
    }).save();
  }
}

Well, that appears to be working simply enough. However, my back-end doesn’t seem to be doing what it’s supposed to and I can’t figure out how to debug it.

How do I look at the response that I get (in Chrome)?

Here’s a snippet from my Laravel code

$v = Validator::make($newuser, $rules, $messages);

if ($v->fails() ) {
return Redirect::to('/register')
	->withErrors($v)
	->withInput();
}

Normally, in Laravel, that would give me an $errors object to use on the page I’m redirected to. How do I look at that error object in Ember? I think it’s in the HTTP response… but I’m not really sure how all of that works. I don’t see it in Chrome dev tools.

:confused:

Okay, sorry, I’m a bit rusty.

So after a little more poking around, knowledge is coming back to me: Laravel sets you up with the errors in a session variable. Does/can Ember.js retrieve and use these session variables?

And/or what are some best practices for doing these kinds of things in Ember?

I.e, I not only want to POST the data, I want some sort of a response to know what happened. Are there errors, was it successful, etc.

I’m not familiar with Laravel, but generally APIs should return proper response codes, like a 500 error if there was a problem on the server-side, or 400 if the request was bad.

If you build an API without conforming to any standards, you’re going to have to write your own adapter and serializer for Ember Data.

This was a major “a-ha” moment for me. I’m still a novice, I guess; I was vaguely aware of how HTTP works and I thought that was enough; and I was wrong. However, after a lot of reading these past couple of days I think I have a much better understanding.

I have successfully figured out how to correctly POST my data to my back-end, validate it, and then either respond with a 422 if it’s invalid, or a 201 if it was successful.

And, along with that 422, I’m sending the errors. I think that I’m on the right track. I found some stuff about an event called “becameInvalid” for handling errors… hard to find the right information because ember-data is in flux.

Tangential question:
So, I have a register form that takes an email and a password, and I send that to the server which does the validation, hashes the password and saves it.

First of all, I’d like to do client-side validation, but that’s not my question (although I can’t find much info about that for Ember):
Should I be doing some sort of security on the password before I send it to the server?
I was trying to read up on this and I saw something about needing SSL/TLS. Doesn’t that cost money? I can’t really find info on how to do it. Is there another way to do that kind of security or is that the standard?