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

Introduction to Ember.js

Introduction to Ember.js

Learn what Ember.js is all about, including core concepts and how to use it.

EmberDallas

August 18, 2014
Tweet

Other Decks in Programming

Transcript

  1. Ember is… • A framework for creating ambitious web applications

    • Built for productivity • Opinionated • Sometimes perceived as difficult to learn (Not to scale)
  2. Models • An object that stores persistent state • Templates

    are responsible for displaying the model to the user by turning it into HTML • In many applications, models are loaded via an HTTP JSON API, although Ember is agnostic to the backend that you choose
  3. Routes • URL representations of your application’s objects, telling the

    template which model it should display (such as a url of /cars renders a collection of cars) • Queries the model and makes it available in the controller and templates • As the templates or models being shown to the user change, Ember automatically keeps the URL in the browser's address bar up-to-date
  4. Routes • This means that, at any point, users are

    able to share the URL of your app. When someone clicks the link, they reliably see the same content as the original user. • Can also… …set properties in Controllers …execute events and actions …connect a particular template to a particular controller
  5. Templates • The HTML (user interface) of your application •

    Each template is backed by a model, accessed via the controller, and the template automatically updates itself if the model changes • Written in the Handlebars templating language • So they can include things such as… …other templates …usual logic such as if and else
  6. Templates …loops …formatting helpers …expressions, like {{firstName}}, which take information

    from the template's model and put it into HTML …outlets, which are placeholders in a template that routers can plug other templates into. You can put outlets into your template using the {{outlet}} helper. …components, custom HTML elements that you can use to clean up repetitive templates or create reusable controls.
  7. Views • Represent the visual parts of your application that

    the user can see in the browser • Associated with a controller, a handlebars template and a route • Handle events or custom interactions that are impossible to manage from templates • didInsertElement hook where can interact with jQuery very easily • Become extremely useful when you need to build reusable views, such as modals, popovers, date-pickers and autocomplete fields
  8. Controllers • Objects that store application state • Have a

    model set on them by a route • Act as bridge between the model and the view or template • A template can retrieve properties from both the model and a controller • Are auto-generated if not explicitly defined
  9. Components • A completely isolated View that has no access

    to the surrounding context • A great way to build reusable components for your applications
  10. Controllers • Allow you to decorate your models with display

    logic • In general, your models will have properties that are saved to the server, while controllers will have properties that your app does not need to save to the server • When a template references a property, the property value on the controller is returned if it exists, otherwise the model assigned to the controller is looked at next
  11. Controllers • There are three different types of controllers: •

    Controller • Value store • ObjectController • Represents a single model • ArrayController • Represents an array of models
  12. Controllers • If you don't specifically define a controller for

    a route, Ember will automatically make one for you based on the return value of the route's model hook • If it returns an object (such as a single record), an ObjectController will be generated • If it returns an array, an ArrayController will be generated • If it does not return anything, an instance of Ember.Controller will be generated
  13. Naming Conventions • When your application boots, Ember will look

    for these objects: • App.ApplicationRoute • App.ApplicationController • the application template
  14. Naming Conventions • If your user navigates to /favorites, Ember

    will look for these objects: • App.FavoritesRoute • App.FavoritesController • the favorites template
  15. Multiple files and directories • Requires being built into single

    application file • Can be done with variety of tools, such as Brunch, Mimosa, Grunt or Broccoli • Strongly recommend Ember CLI
  16. Take Aways • Architecture in Ember applications is dictated by

    routes and templates • Controllers are like decorators for your models; routes are more like traditional controllers for your application • Think long and hard before putting actions on controllers. Should instead put them on the lowest shareable route.