29
Rangular development Using angular with Rails (for Single Page web apps) Jamie Davidson Pathgather // follow me : @jamiehdavidson Thursday, June 20, 13

Using Angular with Rails

Embed Size (px)

DESCRIPTION

A presentation I gave at a Boston Rails Meetup, on using Angular and Rails

Citation preview

Page 1: Using Angular with Rails

Rangular developmentUsing angular with Rails

(for Single Page web apps)

Jamie Davidson

Pathgather // follow me : @jamiehdavidson

Thursday, June 20, 13

Page 2: Using Angular with Rails

Traditional Rails request flow

Pathgather // follow me : @jamiehdavidson

/users/3navigates to

✓ Is route defined in routes.rb?resources :users

- Ensure it’s an HTML request- Run before_filters, authenticate user- fetch the user, store in instance variable

application.html.erb/.haml

show.html.erb/.haml

<h1><%= @user.name %></h1>

Thursday, June 20, 13

Page 3: Using Angular with Rails

With Angular + Rails, this workflow is

usually bypassed....but not always

Thursday, June 20, 13

Page 4: Using Angular with Rails

Step 1: Build an API

‘Shell’ controllers to handle html requests Often bypassed

Pathgather // follow me : @jamiehdavidson

api controllers to handle json requestsNever Bypassed

Thursday, June 20, 13

Page 5: Using Angular with Rails

+Rails Code Structure

Startup Institute boston // follow me : @jamiehdavidson

Few Html requests = Few to none rails helpers

Thursday, June 20, 13

Page 6: Using Angular with Rails

+ API vs HTMl Controller

Pathgather // follow me : @jamiehdavidson

app/controllers/users_controller.rb app/controllers/api/v1/users_controller.rb

* Only exists to accept full HTML requests to /users/:id. Often bypassed

* Separate base controller allows us to specify before_filters and handle errors specific to API requests

Thursday, June 20, 13

Page 7: Using Angular with Rails

Step 2: Define JSON View

‘Shell’ html views to handle html requests Often bypassed

Pathgather // follow me : @jamiehdavidson

json views to define returned json structureNever bypassed

Thursday, June 20, 13

Page 8: Using Angular with Rails

+ RABL

Pathgather // follow me : @jamiehdavidson

app/views/users/show.html.haml app/views/users/show.json.rabl* Totally blank file. Seriously, it exists, but contains

nothing.Maybe there’s a better way???

https://github.com/nesquena/rabl

http://railscasts.com/episodes/322-rablRailscast:

Thursday, June 20, 13

Page 9: Using Angular with Rails

Step 3: Add angular

why angular over the others?

Pathgather // follow me : @jamiehdavidson

how does it change our normal rails-y javscript stucture?

Thursday, June 20, 13

Page 11: Using Angular with Rails

+ Angular App Specification

Restangular over $http or ng-resource (https://github.com/mgonto/restangular)

jst covered in a few

Thursday, June 20, 13

Page 12: Using Angular with Rails

Step 3A: Angular Controllers

sends requests to your rails api,saves response to a scope variable

Pathgather // follow me : @jamiehdavidson

Don’t touch the dom!!! Don’t even query the dom

Creates listeners for changes made by user$watch is your friend

Thursday, June 20, 13

Page 13: Using Angular with Rails

+ Angular Controller + View

app/assets/javascripts/my_app/controllers/user.js.coffee app/assets/javascripts/templates/users/show.hamlc

Note: I don’t like this. USer sees a blank template before seeing the actual user data

Instead, query for your data before the template loads. leads to a smoother transition between views:

Thursday, June 20, 13

Page 14: Using Angular with Rails

+ Handling HTTP ERRORS

No ugly red error pages by default anymore

Need to ‘intercept’ api errors and render an error page

reuse those ugly red pages

What happens when the user makes an api request for data that doesn’t exist or data they aren’t allowed to view?

yourapp.js.coffee

Thursday, June 20, 13

Page 15: Using Angular with Rails

pathgather // follow me : @jamiehdavidson

HTML

https://github.com/netzpirat/haml_coffee_assets

Haml can be used for angular templates

closing html tags

Thursday, June 20, 13

Page 16: Using Angular with Rails

Angular SERVICES

SINGLETONS...VERY IMPORTANT!!

Pathgather // follow me : @jamiehdavidson

move global javascript functions/helpers to services

Can be used as ‘wrappers’ to other services/data

Thursday, June 20, 13

Page 17: Using Angular with Rails

+ Service example: current user

let’s see some real code

Thursday, June 20, 13

Page 18: Using Angular with Rails

Angular Filters

Simply used to format data

Pathgather // follow me : @jamiehdavidson

if you need to format data by rendering/manipulating html, use a directive

Angular ships with many predefined filters:- formatting dates and currency

- searching within an array (see angular docs)- http://www.cheatography.com/proloser/cheat-sheets/angularjs/

Thursday, June 20, 13

Page 19: Using Angular with Rails

Angular Directives

horribly complicated

Pathgather // follow me : @jamiehdavidson

Highly technical documentationBut read, re-read, and re-re-read

absurdly powerfulRegister behavior, transform the DOM, create reusable widgets, etc

Unfortunately, i could spend this entire talk on directives, and you’d probably just leave confused

Angular ships with many out of the boxng-repeat, ng-cloak, ng-view, ng-*

(Create your own prefix. Don’t use ng)

Thursday, June 20, 13

Page 20: Using Angular with Rails

+Directives and jquery plugins

DON’t:

Text

/app/assets/javascripts/[some model].js.coffee

Minimal:

Thursday, June 20, 13

Page 21: Using Angular with Rails

A note about jquery and angular

I find myself very hesitant to use jquery only plugins

Pathgather // follow me : @jamiehdavidson

when starting out with angular, don’t include jquery

‘Roll your own’ first, jquery plugin lastshare your directives on github. sharing is caring!!

set a maximum number (single digits) of jquery-only plugins you’ll use

Thursday, June 20, 13

Page 22: Using Angular with Rails

+ User Authentication/Authorization

Server side authentication doesn’t change (beyond API authentication), but you can’t do this anymore!!

So how do we alter the view to dynamically display html based on who the current user is?

Thursday, June 20, 13

Page 23: Using Angular with Rails

+Angular + Rails user authenticationYou can send AJAX requests, do full page loads, or....

Current user service + directives

But Wait, this is javascript. won’t some average hacker be able to disguise themselves as another user?Yep. But we’re good little rails developers, so we have server-side checks (devise, cancan, etc) to ensure the logged-in user can actually access/modify the data.they can make themselves look like the current user On the client...but they can’t act like them

Thursday, June 20, 13

Page 24: Using Angular with Rails

+ Angular + SEO

What a crawler sees What our user sees

Thursday, June 20, 13

Page 25: Using Angular with Rails

+ Angular + SEO

Manual lifting required

You have to build a pre-rendered version and render that to the crawler only

Stay Tuned for crawler improvements and js-heavy/one page apps

Resources:- http://www.yearofmoo.com/2012/11/angularjs-and-seo.html- https://developers.google.com/webmasters/ajax-crawling

Thursday, June 20, 13

Page 26: Using Angular with Rails

Final Resources

Pathgather // follow me : @jamiehdavidson

Angular UIangular-ui.github.io

angular screencastsegghead.io

railscast

http://www.cheatography.com/proloser/cheat-sheets/angularjs/Cheat Sheet

https://github.com/jmcunningham/AngularJS-LearningGithub repo of learning resources

http://railscasts.com/episodes/405-angularjs

Thursday, June 20, 13

Page 27: Using Angular with Rails

Oh, we’re hiring.

Pathgather // follow me : @jamiehdavidson

Thursday, June 20, 13

Page 28: Using Angular with Rails

THANKS. FOR YOUR ATTENTION

Pathgather // follow me : @jamiehdavidson

Thursday, June 20, 13

Page 29: Using Angular with Rails

Q&Atime for me to shutup and for you to talk

or i can tell you more about directives

Pathgather // follow me : @jamiehdavidson

Thursday, June 20, 13