Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ember on Rails: #REALTALK

Ember on Rails: #REALTALK

If you work in Rails and have ever wondered about Ember.js, you should know that Ember and Rails go together like Nutella and pretzels. (Which is to say, quite well indeed.)

Get an inside look of the experience of going from having never tried Ember to shipping a production application in it. What makes Ember a good match for certain types of applications? What's so different about it compared to other frameworks? What are the drawbacks?

With this quick tour, you'll be equipped to jump in and start seeing the kind of crazy-ambitious applications you can build when you've got Ember.js in your toolset.

Presented at Lone Star Ruby Conf 2013

tehviking

July 19, 2013
Tweet

More Decks by tehviking

Other Decks in Technology

Transcript

  1. MVC

  2. :*(

  3. - Browse tweets - Display story inline - Readability integration

    - Filter tweet types - Mark as read - Filter read/unread - J/K Navigation
  4. JQUERY CASCADE: PLUGIN 1 THAT WAS EASY PLUGIN 2 THAT

    WAS... LESS EASY PLUGIN 3 OH THE HUMANITY
  5. Gemfile gem 'ember-rails' gem 'ember-source', '1.0.0.rc6' from command line: rails

    g ember:bootstrap oh, and SHUT OFF TURBOLINKS INSTALLATION
  6. HOW IT WORKS app/controllers/home_controller.rb class HomeController < ApplicationController def index

    end end config/routes.rb match "/*path" => "home#index", via: %w(get post) root "home#index"
  7. HOW IT WORKS app/assets/javascripts/router.js.coffee App.Router.map -> @route 'home', path: '/'

    app/assets/javascripts/templates/application.handlebars HELLO THERE BIG BEAUTIFUL WORLD! {{outlet}} WE’LL TALK ABOUT {{OUTLET}} IN A MOMENT
  8. MODELS 2 JOBS: DEFINE AND HOLD PROPERTIES FOR DOMAIN MODEL

    INSTANCES RETRIEVE/PERSIST CHANGES IF USING EMBER-DATA OR EMBER-MODEL
  9. ROUTES APP_ROUTER.JS IS WHAT YOU KNOW AS "ROUTES" ROUTES DO

    HEAVY LIFTING. GETS YOUR MODEL OBJECT, STUFFS IT INTO A CONTROLLER, AND PASSES IT ALONG
  10. VIEWS VIEWS != TEMPLATES VIEWS IN RAILS ARE INVISIBLE, THEY

    PASS CONTEXT TO TEMPLATES VIEWS IN EMBER ARE OPTIONAL, FOR HANDLING USER INPUT & DISPLAY LOGIC
  11. KEEPING UP BETAS, RC1, RC2, RC3, RC4, RC5, RC6, RC6.2

    EMBER-DATA BETAS HANDLEBARS BETAS
  12. KEEPING UP BETAS, RC1, RC2, RC3, RC4, RC5, RC6, RC6.2

    EMBER-DATA BETAS HANDLEBARS BETAS W-WH... WHAT?
  13. app/assets/javascripts/routes.js.coffee App.Router.map -> @resource 'sharedItems', path: '/', -> @resource 'sharedItem',

    path: '/:shared_item_id' @route "fourOhFoured", path: "*:" App.Router.reopen location: 'history' DIVING IN: EMBER
  14. app/controllers/shared_items_controller.rb module Api class SharedItemsController < ApplicationController respond_to :json def

    index @shared_items = current_user.shared_items.all respond_with @shared_items, root: false end def show @shared_item = current_user.shared_items.find(params[:id]) respond_with @shared_item, root: false end def update @shared_item = current_user.shared_items.find(params[:id]) if @shared_item.update!(shared_item_params) respond_with @shared_item, root: false end end private def shared_item_params params.require(:shared_item).permit(:read_state) end end end API: IT’S JUST RAILS
  15. app/models/readable_item.rb class ReadableItem attr_accessor :content, :title, :author, :url def initialize(url)

    @url = url get_readable_content end def get_readable_content request = HTTParty.get("http:// foo.readability.com/") readability_response = request.parsed_response self.content = readability_response["content"] self.title = readability_response["title"] self.author = readability_response["author"] end end NON-PERSISTED MODELS
  16. app/assets/javascripts/models/shared_item.js.coffee App.SharedItem = Ember.Model.extend id: Ember.attr() twitter_username: Ember.attr() url: Ember.attr()

    tweet_body: Ember.attr() shared_at: Ember.attr() sharer_avatar_url: Ember.attr() App.SharedItem.url = "/api/shared_items" App.SharedItem.adapter = Ember.RESTAdapter.create() SOLUTION: DATA EMBER-MODEL
  17. app/assets/javascripts/models/shared_item.js.coffee App.SharedItem.find() # Array: shared_items#index App.SharedItem.find(3) # Object: shared_items#show sharedThingy.save()

    # Object: shared_items#create/update ‘FRINSTANCE: App.SharedItemRoute = App.AuthenticatedRoute.extend model: (params) -> App.SharedItem.find(params.shared_item_id) SOLUTION: DATA EMBER-MODEL
  18. DROPS EMBER INTO APP LEAVES PROJECT* *NOT ACTUALLY A SCUMBAG,

    BASICALLY MADE THIS TALK EVEN POSSIBLE
  19. RESOURCES Ember.js tutorial screencast by Tom Dale http://emberjs.com/guides/ Much Very

    Confused: Explaining client-side MVC for the server-side dev http://jeffreybiles.com/blog/much-very-confused.html ember-rails https://github.com/emberjs/ember-rails Pure CSS Framework http://purecss.io/ Tuts+ Let's Learn Ember (FREE!) http://freecourses.tutsplus.com/lets-learn-ember/