SlideShare uma empresa Scribd logo
1 de 30
Baixar para ler offline
SOPHISTICATED
VIEWS
How Ember's View Layer Handles the
Complex Problems
ME
PETER WAGENET
■ Tilde Employee
■ Ember Core Team Member
■ Former SproutCore Core Team Member
■ Ruby on Rails Developer
EMBER
A FRAMEWORK FOR
CREATING AMBITIOUS
WEB APPLICATIONS

            How many here are familiar with Ember?
            What do you all use?
A BRIEF HISTORY
WHY EMBER?
There’s a myriad of      ■ No trivial choices
JavaScript application
                         ■ Write less code
frameworks. Here are
some reasons to pick     ■ We solve the complex
Ember.                     problems
                                       I’d rather sell you on philosophy than
                                       features. It’s more important for you to
                                       know how we approach problems.
THE
COMPLEX
PROBLEMS
SOME PROBLEMS
There are a number of    ■ Managing Nested Views
complex problems that
                         ■ Handling Events
Ember tackles. We’ll
focus on two of them            Event handling is in some ways a subset of
                                nested views, but is a large enough topic to be
                                addressed on its own.
which are pretty
closely tied together.
MANAGING
NESTED VIEWS
While writing to the       Problems
DOM doesn’t seem
                           ■ Zombie Views and Events
too hard, once you
start nesting views it’s   ■ Re-rendering
easy to trip up.
ZOMBIES

          Zombies - views or events that don’t die

          Usually this happens because a parent re-
          rendered or was destroyed and forgot to
          teardown the child.

          In the case of events, an event handler
          was set up and never removed.
EVENTS
destroy: function () {
  var c = this; this.unbind();
  try {
    this._element.pause(),
    this._element.removeEventListener("error",
                                       this._triggerError),
    this._element.removeEventListener("ended", this._triggerEnd),
    this._element.removeEventListener("canplay",
                                       this._triggerReady),
    this._element.removeEventListener("loadedmetadata",
                                       this._onLoadedMetadata),
    _.each(b, function (a) {
      c._element.removeEventListener(a, c._bubbleProfilingEvent)
    }),
    _.each(a, function (a) {
      c._element.removeEventListener(a, c._logEvent)
    }),
    this._element = null, this.trigger("destroy")
  } catch (d) {}
                                                 This is from Rdio’s app

                                                 The point is not to knock on Backbone,
                                                 but to realize that there’s a lot to
                                                 remember about and it’s easy to miss
                                                 things. This is how zombies happen.
VIEWS




If we want to open at 8am, we need to re-
render the App View. In order to re-render
the App View, the App View must also         The issue here occurs if we re-render the
manually re-render the child views and re-   App View and don’t properly tear-down
insert them into App View's element.         the Menu and Menu Item views. We
                                             could easily end up with Zombie events
                                             and memory leaks.
HANDLING EVENTS
Web applications need    ■ Inconsistent Behavior
to handle a wide
                         ■ Setup and Teardown
variety of DOM
events. It’s easy to     ■ Performance
handle just a few, but
                                                   Sort of a continuation
problems arise with                                from the last point.


more.
Lots of individual handlers can be slow.
Not to mention the fact that you have to set
up different handlers for each event type.
SOLUTIONS
BUILT-IN
CHILD VIEWS
Views are fully aware   ■ Smarter re-rendering
of their children,
                        ■ Automatic binding and
which means less
                          observer cleanup
work for your app.
In this example, you can see that the views are
nested. If we need to re-render the AppView,
Ember tearsdown the children and then re-
renders them at the appropriate time.

Ember cleans up any bindings and obser vers for
views (and other objects) when destroyed.

As it turns out event teardown doesn’t really
matter.
JQUERY
One of the many          ■ Regularizes DOM event
things jQuery does for     bubbling
Ember is help to
                         ■ Provides the `on` method for
normalize event            delegation
handling.




                                 Doesn’t quite merit its own slide, but it is worth
                                 mentioning that we outsource tough problems when we
                                 can. We’re a bit lazy, even if it doesn’t look like it. We
                                 don’t want to deal with problems if someone else
                                 already does it well.
EVENT DELEGATION
Ember implements an    ■ Single Event Handler
event delegator that
                       ■ Passes events to correct view
handles all inbound
events and sends       ■ Event setup and teardown
them the correct
location.
Individual views don’t actually have any event
listeners. We just assign an `ember-view` class
to them that allows jQuery.on to register their
events. No worries about zombie events here.
MORE
SOLUTIONS
HANDLEBARS
Integrated Handlebars   ■ Child views
helpers allow you to
                        ■ Event helpers
write less code
without losing any of   ■ Bound properties
Ember’s benefits.
VIRTUAL VIEWS
Ember uses a purpose    ■ Allows for binding content to
built library called      the DOM
Metamorph for
                        ■ No use of HTML elements
updating parts of the
DOM.                    ■ Doesn’t affect CSS
Handlebars
<div id="name">
  {{name}}
</div>


                    HTML
<div id="name">
  <script id="metamorph−5−start" type="text/x
−placeholder"></script>
    John Doe
  <script id="metamorph−5−end" type="text/x
−placeholder"></script>
</div>
END
EMBERJS.COM
@WAGENET

Mais conteúdo relacionado

Semelhante a Ember - Sophisticated Views

The State of Front-end At CrowdTwist
The State of Front-end At CrowdTwistThe State of Front-end At CrowdTwist
The State of Front-end At CrowdTwist
Mark Fayngersh
 
Intro to BackboneJS + Intermediate Javascript
Intro to BackboneJS + Intermediate JavascriptIntro to BackboneJS + Intermediate Javascript
Intro to BackboneJS + Intermediate Javascript
Andrew Lovett-Barron
 

Semelhante a Ember - Sophisticated Views (20)

Building Rich User Experiences Without JavaScript Spaghetti
Building Rich User Experiences Without JavaScript SpaghettiBuilding Rich User Experiences Without JavaScript Spaghetti
Building Rich User Experiences Without JavaScript Spaghetti
 
Mobile optimization
Mobile optimizationMobile optimization
Mobile optimization
 
Intro to react_v2
Intro to react_v2Intro to react_v2
Intro to react_v2
 
Ryan Christiani I Heard React Was Good
Ryan Christiani I Heard React Was GoodRyan Christiani I Heard React Was Good
Ryan Christiani I Heard React Was Good
 
Marionette - TorontoJS
Marionette - TorontoJSMarionette - TorontoJS
Marionette - TorontoJS
 
"Crafting a Third-Party Banking Library with Web Components and React", Germa...
"Crafting a Third-Party Banking Library with Web Components and React", Germa..."Crafting a Third-Party Banking Library with Web Components and React", Germa...
"Crafting a Third-Party Banking Library with Web Components and React", Germa...
 
Getting Started with React-Nathan Smith
Getting Started with React-Nathan SmithGetting Started with React-Nathan Smith
Getting Started with React-Nathan Smith
 
Backbone.js – an introduction
Backbone.js – an introductionBackbone.js – an introduction
Backbone.js – an introduction
 
Win8 development lessons learned jayway
Win8 development lessons learned jaywayWin8 development lessons learned jayway
Win8 development lessons learned jayway
 
React js Rahil Memon
React js Rahil MemonReact js Rahil Memon
React js Rahil Memon
 
Ruby For Startups
Ruby For StartupsRuby For Startups
Ruby For Startups
 
AngularJS preso with directives and route resolve
AngularJS preso with directives and route resolveAngularJS preso with directives and route resolve
AngularJS preso with directives and route resolve
 
The State of Front-end At CrowdTwist
The State of Front-end At CrowdTwistThe State of Front-end At CrowdTwist
The State of Front-end At CrowdTwist
 
Embracing Uncertainty: Learning to Think Responsively
Embracing Uncertainty: Learning to Think ResponsivelyEmbracing Uncertainty: Learning to Think Responsively
Embracing Uncertainty: Learning to Think Responsively
 
Nagios Conference 2012 - Nathan Vonnahme - Monitoring the User Experience
Nagios Conference 2012 - Nathan Vonnahme - Monitoring the User ExperienceNagios Conference 2012 - Nathan Vonnahme - Monitoring the User Experience
Nagios Conference 2012 - Nathan Vonnahme - Monitoring the User Experience
 
Windows Store apps - Lessons Learned
Windows Store apps - Lessons LearnedWindows Store apps - Lessons Learned
Windows Store apps - Lessons Learned
 
Getting Started with React
Getting Started with ReactGetting Started with React
Getting Started with React
 
Effective debugging
Effective debuggingEffective debugging
Effective debugging
 
Conquering Gef Part 1: Effectively creating a well designed graphical editor
Conquering Gef Part 1: Effectively creating a well designed graphical editorConquering Gef Part 1: Effectively creating a well designed graphical editor
Conquering Gef Part 1: Effectively creating a well designed graphical editor
 
Intro to BackboneJS + Intermediate Javascript
Intro to BackboneJS + Intermediate JavascriptIntro to BackboneJS + Intermediate Javascript
Intro to BackboneJS + Intermediate Javascript
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

Ember - Sophisticated Views

  • 1. SOPHISTICATED VIEWS How Ember's View Layer Handles the Complex Problems
  • 2.
  • 3. ME
  • 4. PETER WAGENET ■ Tilde Employee ■ Ember Core Team Member ■ Former SproutCore Core Team Member ■ Ruby on Rails Developer
  • 6. A FRAMEWORK FOR CREATING AMBITIOUS WEB APPLICATIONS How many here are familiar with Ember? What do you all use?
  • 8. WHY EMBER? There’s a myriad of ■ No trivial choices JavaScript application ■ Write less code frameworks. Here are some reasons to pick ■ We solve the complex Ember. problems I’d rather sell you on philosophy than features. It’s more important for you to know how we approach problems.
  • 10. SOME PROBLEMS There are a number of ■ Managing Nested Views complex problems that ■ Handling Events Ember tackles. We’ll focus on two of them Event handling is in some ways a subset of nested views, but is a large enough topic to be addressed on its own. which are pretty closely tied together.
  • 11. MANAGING NESTED VIEWS While writing to the Problems DOM doesn’t seem ■ Zombie Views and Events too hard, once you start nesting views it’s ■ Re-rendering easy to trip up.
  • 12. ZOMBIES Zombies - views or events that don’t die Usually this happens because a parent re- rendered or was destroyed and forgot to teardown the child. In the case of events, an event handler was set up and never removed.
  • 13. EVENTS destroy: function () { var c = this; this.unbind(); try { this._element.pause(), this._element.removeEventListener("error", this._triggerError), this._element.removeEventListener("ended", this._triggerEnd), this._element.removeEventListener("canplay", this._triggerReady), this._element.removeEventListener("loadedmetadata", this._onLoadedMetadata), _.each(b, function (a) { c._element.removeEventListener(a, c._bubbleProfilingEvent) }), _.each(a, function (a) { c._element.removeEventListener(a, c._logEvent) }), this._element = null, this.trigger("destroy") } catch (d) {} This is from Rdio’s app The point is not to knock on Backbone, but to realize that there’s a lot to remember about and it’s easy to miss things. This is how zombies happen.
  • 14. VIEWS If we want to open at 8am, we need to re- render the App View. In order to re-render the App View, the App View must also The issue here occurs if we re-render the manually re-render the child views and re- App View and don’t properly tear-down insert them into App View's element. the Menu and Menu Item views. We could easily end up with Zombie events and memory leaks.
  • 15. HANDLING EVENTS Web applications need ■ Inconsistent Behavior to handle a wide ■ Setup and Teardown variety of DOM events. It’s easy to ■ Performance handle just a few, but Sort of a continuation problems arise with from the last point. more.
  • 16. Lots of individual handlers can be slow. Not to mention the fact that you have to set up different handlers for each event type.
  • 18. BUILT-IN CHILD VIEWS Views are fully aware ■ Smarter re-rendering of their children, ■ Automatic binding and which means less observer cleanup work for your app.
  • 19. In this example, you can see that the views are nested. If we need to re-render the AppView, Ember tearsdown the children and then re- renders them at the appropriate time. Ember cleans up any bindings and obser vers for views (and other objects) when destroyed. As it turns out event teardown doesn’t really matter.
  • 20. JQUERY One of the many ■ Regularizes DOM event things jQuery does for bubbling Ember is help to ■ Provides the `on` method for normalize event delegation handling. Doesn’t quite merit its own slide, but it is worth mentioning that we outsource tough problems when we can. We’re a bit lazy, even if it doesn’t look like it. We don’t want to deal with problems if someone else already does it well.
  • 21. EVENT DELEGATION Ember implements an ■ Single Event Handler event delegator that ■ Passes events to correct view handles all inbound events and sends ■ Event setup and teardown them the correct location.
  • 22. Individual views don’t actually have any event listeners. We just assign an `ember-view` class to them that allows jQuery.on to register their events. No worries about zombie events here.
  • 24.
  • 25. HANDLEBARS Integrated Handlebars ■ Child views helpers allow you to ■ Event helpers write less code without losing any of ■ Bound properties Ember’s benefits.
  • 26. VIRTUAL VIEWS Ember uses a purpose ■ Allows for binding content to built library called the DOM Metamorph for ■ No use of HTML elements updating parts of the DOM. ■ Doesn’t affect CSS
  • 27. Handlebars <div id="name"> {{name}} </div> HTML <div id="name"> <script id="metamorph−5−start" type="text/x −placeholder"></script> John Doe <script id="metamorph−5−end" type="text/x −placeholder"></script> </div>
  • 28. END