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

Ember: The Magic, Demystified

Ember: The Magic, Demystified

How do the names that you provide to Ember actually end up resulting in Ember looking for a given route/controller/view/template combination? How does this apply to components and their templates? This talk reviews the Ember internals to illuminate that this isn't magical, but quite straight-forward.

Robert Jackson

June 17, 2014
Tweet

More Decks by Robert Jackson

Other Decks in Programming

Transcript

  1. Who the heck is this guy? • Ember Core Team

    • General OSS addict • DockYarder twitter: rwjblue github: rjackson
  2. Route Lookup var dsl = DSL.map(function() { this.resource( 'application', {

    path: "/" }, function() { callback.call(this); }); }); Ember.Router.map
  3. Route Lookup var dsl = DSL.map(function() { this.resource( 'application', {

    path: "/" }, function() { callback.call(this); }); }); Ember.Router.map
  4. Route Lookup var R = container.lookupFactory('route:basic') return function(name) { var

    handler = this.container.lookup('route:' + name); handler.routeName = name; if (handler) { return handler;} this.container.register('route:' + name, R.extend()); return this.container.lookup('route:' + name); }; Ember.Router#_getHandlerFunction
  5. Route Lookup • Router.map sets up the handlers • router.js

    handles the actual route transitions • New route is entered: getHandler(‘posts’) • So we have a Route instance, now what?
  6. Route Rendering • router.js invokes various async hooks (activate, model,

    redirect, etc) • router.js invokes route.setup
  7. Route Rendering var controllerName = this.controllerName || this.routeName, controller =

    this.controllerFor(controllerName, true); if (!controller) { controller = this.generateController(controllerName,context); } Ember.Route#setup
  8. Route Rendering var name = this.routeName; var templateName = this.templateName

    || name; var viewName = this.viewName || name; Ember.Route#render
  9. Route Rendering var view = container.lookup('view:' + viewName), template =

    view ? view.get('template') : null; if (!template) { template = container.lookup('template:' + templateName); } Ember.Route#render
  10. Route Rendering options = normalizeOptions(this, name, template, options); view =

    setupView(view, container, options); appendView(this, view, options); Ember.Route#render
  11. Route Lookup // packages/ember-routing/lib/system/route.js#L696 // Ember.Route#setup controller = this.controllerFor(controllerName, true);

    if (!controller) { controller = this.generateController(controllerName,context); } app/controllers/posts.js
  12. Route Lookup // packages/ember-routing/lib/system/route.js#L1377 // Ember.Route#render view = container.lookup('view:' +

    viewName), view = setupView(view, container, options); appendView(this, view, options); app/views/posts.js
  13. Route Lookup app/templates/posts.hbs // packages/ember-routing/lib/system/route.js#L1381 // Ember.Route#render template = view

    ? view.get('template') : null; template = template || container.lookup('template:' + templateName); options = normalizeOptions(this, name, template, options); view = setupView(view, container, options);
  14. export default Ember.Component.extend({ foobar: Ember.computed('bar', function() { return 'foo' +

    Ember.get(this, 'bar'); }) }); Component Lookup Component: x-foo
  15. var id = new Handlebars.AST.IdNode([{ part: '_triageMustache' }]); mustache =

    new Handlebars.AST.MustacheNode( [id].concat([mustache.id]), mustache.hash, !mustache.escaped ); return Handlebars.Compiler.prototype.mustache.call(this, mustache); Component Lookup Ember.Handlebars.Compiler.mustache
  16. var helper = EmberHandlebars.resolveHelper( options.data.view.container, property ); if (helper) {

    return helper.call(this, options); } Component Lookup _triageMustacheHelper
  17. var componentLookup = container.lookup('component-lookup:main'); var Component = componentLookup.lookupFactory(name, container); if

    (Component) { helper = EmberHandlebars.makeViewHelper(Component); container.register('helper:' + name, helper); } Component Lookup resolveHelper
  18. Component Lookup Em.ComponentLookup.lookupFactory var fullName = 'component:' + name, templateFullName

    = 'template:components/' + name, templateRegistered = this.container && this.container.has(templateFullName); if (templateRegistered) container.injection(fullName, 'layout', templateFullName);
  19. Component Lookup Em.ComponentLookup.lookupFactory var Component = container.lookupFactory(fullName); if (templateRegistered ||

    Component) if (!Component) { container.register(fullName, Ember.Component); Component = container.lookupFactory(fullName); }