SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
Attribute Actions
github.com/emberjs/rfcs/pull/100
WHY
Some actions bubble
<button {{action 'save'}}></button>
Some actions don’t
{{my-button onclick=(action ‘save')}}
Many ways to invoke an action
called from return value?
works w/ strings
actions?
this.sendAction(
‘actionName’
);
component no yes
this.send(
‘action’
);
controller/route no n/a
this.get(
‘actionName’
)();
component yes no
Old actions were bad
String passing
No return values
Lazy execution-time errors about a missing action
No event
Closure actions are better
Function passing, closures
Return values
Render-time errors about a missing action
Event is the trigger argument
This is the only official public API for
attaching events to DOM
<button {{action 'save'}}></button>
To get consistent behavior people do this
<button {{action (action ‘save’)}}></button>
Attribute actions are an official public API
that has these features for DOM events
Function passing, closures
Return values
Render-time errors about a missing action
Event is trigger argument
PROPOSED BEHAVIOR (NOT SYNTAX)
<button {{better-action ‘save’ a b on=“click”}}></button>
Strawman syntax. Not a real proposal.
Always includes event name, “click”
Accepts string, which create a closure off
actions hash, or accepts a function
<button onclick={{action ‘save’ a b}}></button>
Behavior A
actions use addEventListener to attach to the
bubbling phase
• Web components may emit custom events. These must
be caught. Setting a handler to a property would not
catch them.
• Some events (focusin) do not call property-based
handlers
• handlers set via property (onclick=) are added to the
bubbling phase
• Merge strategy for Glimmer components is natural, both
the root element and the invocation attach listeners.
Dispatching events is the only way events
should send data to framework handlers
DANGER OPINIONS
• Web component and built-in elements should act similar.
Both should have an event object.

<my-wc-button {{better-action ‘bar’ on=“snap”}}> <- should
be able to expect an event regardless of if “snap” is
native. IMO.
• React has popularized “just pass a function”. WC written
this way are limited (cannot bubble, cannot be
cancelable). They couple invocation to implementation.
• We can install a convenient dispatcher. node.onsnap =
node.onsnap || (…arguments) => node.dispatchEvent(new
CustomEvent(‘snap’, {detail: arguments}))
Behavior B
actions are called with the event
• Devs want to access the event object
• Closure actions already have value= which is sugar to
help read properties off events
• dispatched events can only emit an event, they cannot
pass multiple arguments
SYNTAX OPTIONS
<button onclick={{action ‘save'}}></button>
• Matches what many people do today
• no special rules for the event name (capitalization,
dashes etc)
• How does oncustom= work? All on* are treated like event
handlers (only=)? Configurable whitelist?
plain property
<button on-click={{action ‘save'}}></button>
• A special rule for naming. Put on- at the start
• of the event name
• Not what people do today
• If cargo-culted into glimmer components,
• annoying to this.attrs[‘on-foo’] vs this.attrs.onfoo.
kebab property

Contenu connexe

Tendances

Tendances (20)

JS Chicago Meetup 2/23/16 - Redux & Routes
JS Chicago Meetup 2/23/16 - Redux & RoutesJS Chicago Meetup 2/23/16 - Redux & Routes
JS Chicago Meetup 2/23/16 - Redux & Routes
 
Railsconf 2017 - React & React Native a common codebase across native and web
Railsconf 2017 - React & React Native a common codebase across native and webRailsconf 2017 - React & React Native a common codebase across native and web
Railsconf 2017 - React & React Native a common codebase across native and web
 
Rethinking Best Practices
Rethinking Best PracticesRethinking Best Practices
Rethinking Best Practices
 
ProvJS: Six Months of ReactJS and Redux
ProvJS:  Six Months of ReactJS and ReduxProvJS:  Six Months of ReactJS and Redux
ProvJS: Six Months of ReactJS and Redux
 
JOSA TechTalks - Better Web Apps with React and Redux
JOSA TechTalks - Better Web Apps with React and ReduxJOSA TechTalks - Better Web Apps with React and Redux
JOSA TechTalks - Better Web Apps with React and Redux
 
Advanced redux
Advanced reduxAdvanced redux
Advanced redux
 
React.js+Redux Workshops
React.js+Redux WorkshopsReact.js+Redux Workshops
React.js+Redux Workshops
 
React on Rails - RailsConf 2017 (Phoenix)
 React on Rails - RailsConf 2017 (Phoenix) React on Rails - RailsConf 2017 (Phoenix)
React on Rails - RailsConf 2017 (Phoenix)
 
Introduction to ReactJS and Redux
Introduction to ReactJS and ReduxIntroduction to ReactJS and Redux
Introduction to ReactJS and Redux
 
Better React state management with Redux
Better React state management with ReduxBetter React state management with Redux
Better React state management with Redux
 
Introduction to React & Redux
Introduction to React & ReduxIntroduction to React & Redux
Introduction to React & Redux
 
Reduxing like a pro
Reduxing like a proReduxing like a pro
Reduxing like a pro
 
New improvements for web developers - frontend.fi, Helsinki
New improvements for web developers - frontend.fi, HelsinkiNew improvements for web developers - frontend.fi, Helsinki
New improvements for web developers - frontend.fi, Helsinki
 
Event sourcing with reactor and spring statemachine
Event sourcing with reactor and spring statemachineEvent sourcing with reactor and spring statemachine
Event sourcing with reactor and spring statemachine
 
Getting started with Redux js
Getting started with Redux jsGetting started with Redux js
Getting started with Redux js
 
API Days Paris - Automatic Testing of (RESTful) API Documentation
API Days Paris - Automatic Testing of (RESTful) API DocumentationAPI Days Paris - Automatic Testing of (RESTful) API Documentation
API Days Paris - Automatic Testing of (RESTful) API Documentation
 
Introduction to React and Flux (CodeLabs)
Introduction to React and Flux (CodeLabs)Introduction to React and Flux (CodeLabs)
Introduction to React and Flux (CodeLabs)
 
Better web apps with React and Redux
Better web apps with React and ReduxBetter web apps with React and Redux
Better web apps with React and Redux
 
You Know WebOS
You Know WebOSYou Know WebOS
You Know WebOS
 
ASP.NET MVC Internals
ASP.NET MVC InternalsASP.NET MVC Internals
ASP.NET MVC Internals
 

En vedette (7)

Demi-PDF
Demi-PDFDemi-PDF
Demi-PDF
 
From 0 to Ember
From 0 to EmberFrom 0 to Ember
From 0 to Ember
 
EmberJS
EmberJSEmberJS
EmberJS
 
Intro to Ember.JS 2016
Intro to Ember.JS 2016Intro to Ember.JS 2016
Intro to Ember.JS 2016
 
Intro to Ember.js
Intro to Ember.jsIntro to Ember.js
Intro to Ember.js
 
An introduction to Ember.js
An introduction to Ember.jsAn introduction to Ember.js
An introduction to Ember.js
 
Choosing the Right Framework for Running Docker Containers in Prod
Choosing the Right Framework for Running Docker Containers in ProdChoosing the Right Framework for Running Docker Containers in Prod
Choosing the Right Framework for Running Docker Containers in Prod
 

Similaire à Attribute actions

jQuery 1.7 Events
jQuery 1.7 EventsjQuery 1.7 Events
jQuery 1.7 Events
dmethvin
 

Similaire à Attribute actions (20)

JavaScript: Events Handling
JavaScript: Events HandlingJavaScript: Events Handling
JavaScript: Events Handling
 
Javascript Browser Events.pdf
Javascript Browser Events.pdfJavascript Browser Events.pdf
Javascript Browser Events.pdf
 
Even more java script best practices
Even more java script best practicesEven more java script best practices
Even more java script best practices
 
jQuery 1.7 Events
jQuery 1.7 EventsjQuery 1.7 Events
jQuery 1.7 Events
 
Client Web
Client WebClient Web
Client Web
 
ReactJS
ReactJSReactJS
ReactJS
 
Mastering the Lightning Framework - Part 2
Mastering the Lightning Framework - Part 2Mastering the Lightning Framework - Part 2
Mastering the Lightning Framework - Part 2
 
Events
EventsEvents
Events
 
PROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part IIPROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part II
 
Intro To webOS
Intro To webOSIntro To webOS
Intro To webOS
 
The Fine Art of JavaScript Event Handling
The Fine Art of JavaScript Event HandlingThe Fine Art of JavaScript Event Handling
The Fine Art of JavaScript Event Handling
 
Will your code blend? : Toronto Code Camp 2010 : Barry Gervin
Will your code blend? : Toronto Code Camp 2010 : Barry GervinWill your code blend? : Toronto Code Camp 2010 : Barry Gervin
Will your code blend? : Toronto Code Camp 2010 : Barry Gervin
 
B2. activity and intent
B2. activity and intentB2. activity and intent
B2. activity and intent
 
PWA 與 Service Worker
PWA 與 Service WorkerPWA 與 Service Worker
PWA 與 Service Worker
 
Javascript and DOM
Javascript and DOMJavascript and DOM
Javascript and DOM
 
JavaScript patterns chapter 8 of mine
JavaScript patterns chapter 8 of mineJavaScript patterns chapter 8 of mine
JavaScript patterns chapter 8 of mine
 
Mobile optimization
Mobile optimizationMobile optimization
Mobile optimization
 
Awt event
Awt eventAwt event
Awt event
 
Dancing with websocket
Dancing with websocketDancing with websocket
Dancing with websocket
 
Learning Svelte
Learning SvelteLearning Svelte
Learning Svelte
 

Plus de Matthew Beale

Scalable vector ember
Scalable vector emberScalable vector ember
Scalable vector ember
Matthew Beale
 

Plus de Matthew Beale (15)

Ember.js Module Loading
Ember.js Module LoadingEmber.js Module Loading
Ember.js Module Loading
 
LA Ember.js Meetup, Jan 2017
LA Ember.js Meetup, Jan 2017LA Ember.js Meetup, Jan 2017
LA Ember.js Meetup, Jan 2017
 
Interoperable Component Patterns
Interoperable Component PatternsInteroperable Component Patterns
Interoperable Component Patterns
 
Ember Community 2016 - Be the Bark
Ember Community 2016 - Be the BarkEmber Community 2016 - Be the Bark
Ember Community 2016 - Be the Bark
 
The Hidden Power of HTMLBars (or, Scope in Ember.js Templates)
The Hidden Power of HTMLBars (or, Scope in Ember.js Templates)The Hidden Power of HTMLBars (or, Scope in Ember.js Templates)
The Hidden Power of HTMLBars (or, Scope in Ember.js Templates)
 
Aligning Ember.js with Web Standards
Aligning Ember.js with Web StandardsAligning Ember.js with Web Standards
Aligning Ember.js with Web Standards
 
New Component Patterns in Ember.js
New Component Patterns in Ember.jsNew Component Patterns in Ember.js
New Component Patterns in Ember.js
 
Scalable vector ember
Scalable vector emberScalable vector ember
Scalable vector ember
 
Testing Ember Apps: Managing Dependency
Testing Ember Apps: Managing DependencyTesting Ember Apps: Managing Dependency
Testing Ember Apps: Managing Dependency
 
Parse Apps with Ember.js
Parse Apps with Ember.jsParse Apps with Ember.js
Parse Apps with Ember.js
 
Snappy Means Happy: Performance in Ember Apps
Snappy Means Happy: Performance in Ember AppsSnappy Means Happy: Performance in Ember Apps
Snappy Means Happy: Performance in Ember Apps
 
Client-side Auth with Ember.js
Client-side Auth with Ember.jsClient-side Auth with Ember.js
Client-side Auth with Ember.js
 
Containers & Dependency in Ember.js
Containers & Dependency in Ember.jsContainers & Dependency in Ember.js
Containers & Dependency in Ember.js
 
Complex Architectures in Ember
Complex Architectures in EmberComplex Architectures in Ember
Complex Architectures in Ember
 
Ember and containers
Ember and containersEmber and containers
Ember and containers
 

Dernier

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
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
 

Dernier (20)

HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
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
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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...
 
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...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
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...
 

Attribute actions

  • 2. WHY
  • 3. Some actions bubble <button {{action 'save'}}></button> Some actions don’t {{my-button onclick=(action ‘save')}}
  • 4. Many ways to invoke an action called from return value? works w/ strings actions? this.sendAction( ‘actionName’ ); component no yes this.send( ‘action’ ); controller/route no n/a this.get( ‘actionName’ )(); component yes no
  • 5. Old actions were bad String passing No return values Lazy execution-time errors about a missing action No event
  • 6. Closure actions are better Function passing, closures Return values Render-time errors about a missing action Event is the trigger argument
  • 7. This is the only official public API for attaching events to DOM <button {{action 'save'}}></button>
  • 8. To get consistent behavior people do this <button {{action (action ‘save’)}}></button>
  • 9. Attribute actions are an official public API that has these features for DOM events Function passing, closures Return values Render-time errors about a missing action Event is trigger argument
  • 11. <button {{better-action ‘save’ a b on=“click”}}></button> Strawman syntax. Not a real proposal. Always includes event name, “click” Accepts string, which create a closure off actions hash, or accepts a function <button onclick={{action ‘save’ a b}}></button>
  • 12. Behavior A actions use addEventListener to attach to the bubbling phase • Web components may emit custom events. These must be caught. Setting a handler to a property would not catch them. • Some events (focusin) do not call property-based handlers • handlers set via property (onclick=) are added to the bubbling phase • Merge strategy for Glimmer components is natural, both the root element and the invocation attach listeners.
  • 13. Dispatching events is the only way events should send data to framework handlers DANGER OPINIONS • Web component and built-in elements should act similar. Both should have an event object.
 <my-wc-button {{better-action ‘bar’ on=“snap”}}> <- should be able to expect an event regardless of if “snap” is native. IMO. • React has popularized “just pass a function”. WC written this way are limited (cannot bubble, cannot be cancelable). They couple invocation to implementation. • We can install a convenient dispatcher. node.onsnap = node.onsnap || (…arguments) => node.dispatchEvent(new CustomEvent(‘snap’, {detail: arguments}))
  • 14. Behavior B actions are called with the event • Devs want to access the event object • Closure actions already have value= which is sugar to help read properties off events • dispatched events can only emit an event, they cannot pass multiple arguments
  • 16. <button onclick={{action ‘save'}}></button> • Matches what many people do today • no special rules for the event name (capitalization, dashes etc) • How does oncustom= work? All on* are treated like event handlers (only=)? Configurable whitelist? plain property
  • 17. <button on-click={{action ‘save'}}></button> • A special rule for naming. Put on- at the start • of the event name • Not what people do today • If cargo-culted into glimmer components, • annoying to this.attrs[‘on-foo’] vs this.attrs.onfoo. kebab property