Skip to content

Instantly share code, notes, and snippets.

@JoshReedSchramm
Forked from joefiorini/1-signin.js
Last active December 17, 2015 13:29
Show Gist options
  • Save JoshReedSchramm/5617045 to your computer and use it in GitHub Desktop.
Save JoshReedSchramm/5617045 to your computer and use it in GitHub Desktop.
// How you could do what you want in Ember
App.Router.map(function(){
this.resource("userSession", function(){
this.route("new", { path: "/sign-in" });
})
});
// However, any time you create a resource you have an extra layer of routing
// & a model that you may/may not want. In the case of sign-in, which doesn't
// really represent part of the UI, I'd consider just using a route instead.
App.Router.map(function(){
this.route("signIn", { path: "/sign-in" });
});
// Then in your controller you can set the current session on ApplicationController
App.SignInController = Ember.Controller.extend({
signIn: function(){
var session = UserSession.validate(this.get("username"), this.get("password")); // assuming you have some object with a session token or something
this.controllers("application").set("session", sesssion);
}
});
<label>
Email:
{{view Ember.TextField valueBinding="username"}}
</label>
<label>
Password:
{{view Ember.TextField valueBinding="password" type="password"}}
</label>
<button {{action signIn}}>Sign In</button>
# app/assets/javascripts/router.js.coffee
Projectile.Router.map (match) ->
this.route("signIn", { path: "/sign-in" })
# app/assets/javascripts/controllers/signInController.js.coffee
Projectile.SignInController = Ember.Controller.extend
signIn: ->
console.log(this.get("email"))
console.log(this.get("password"))
session = Projectile.userSession.signIn(this.get("email"), this.get("password"))
#this.controllers("application").set("session", session)
# app/assets/javascripts/models/userSession.js.coffee
Projectile.UserSession = Ember.Object.extend
# has a signIn method that's getting called propertly.
Projectile.userSession = Projectile.UserSession.create()
# app/assets/javascripts/templates/signIn.handlebars
<div>
<label {{bindAttr for="emailField.elementId"}}>Email</label>
{{view Ember.TextField valueBinding='email'}}
</div>
<div>
<label {{bindAttr for="passwordField.elementId"}}>Password</label>
{{view Ember.TextField valueBinding='password' type='password'}}
</div>
<div>
<button {{action signIn}}>Login</button>
</div>
@JoshReedSchramm
Copy link
Author

This version is getting the form to display, routing properly and even calls into the user session object and hitting the server. all awesome.

problem - the 2 console.logs spit out "undefined". I can't get either the email or the password.

@JoshReedSchramm
Copy link
Author

also just realized those labels won't work that way but i can fix that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment