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

ember-string-interpolate

Jay Phelps
January 14, 2014

 ember-string-interpolate

https://github.com/jayphelps/ember.string.interpolate

Adds string interpolation as a computed property to Ember.js (i.e. no more unreadable getter concatenation).

This was the slideshow for my Ember.js South California Meetup talk.

Sample code: https://gist.github.com/jayphelps/8431829

Jay Phelps

January 14, 2014
Tweet

More Decks by Jay Phelps

Other Decks in Programming

Transcript

  1. WHO I AM • CTO at Pivotshare • Loves code,

    hates condiments. Jay Phelps
  2. WHO I AM • CTO at Pivotshare • Loves code,

    hates condiments. Jay Phelps github: @jayphelps twitter: @_jayphelps
  3. THE “PROBLEM” • Create a string from a mix of

    pre-defined and runtime strings.
  4. THE “PROBLEM” • Create a string from a mix of

    pre-defined and runtime strings. • Concatenating a bunch of this.get(‘key’) calls with strings is menial but can quickly become difficult to read.
  5. THE “PROBLEM” • Create a string from a mix of

    pre-defined and runtime strings. • Concatenating a bunch of this.get(‘key’) calls with strings is menial but can quickly become difficult to read. • Requires Boilerplate
  6. THE “PROBLEM” • Create a string from a mix of

    pre-defined and runtime strings. • Concatenating a bunch of this.get(‘key’) calls with strings is menial but can quickly become difficult to read. • Requires Boilerplate • Not pretty
  7. STRING INTERPOLATION • A prefix token is used to identify

    keys that should be replaced with that variables value. (i.e. a placeholder)
  8. STRING INTERPOLATION • A prefix token is used to identify

    keys that should be replaced with that variables value. (i.e. a placeholder) • Dollar sign ($), hash (#) are some of the most common tokens
  9. STRING INTERPOLATION • A prefix token is used to identify

    keys that should be replaced with that variables value. (i.e. a placeholder) • Dollar sign ($), hash (#) are some of the most common tokens • For Ember, we can create a computed property that binds these keys to the values on the current context. (this)
  10. WHAT DOES IT GIVE US? • One liners • $keys

    are automatically observed and the string recomputed on changes
  11. WHAT DOES IT GIVE US? • One liners • $keys

    are automatically observed and the string recomputed on changes • Returns a computed property, so you can chain.readOnly(), .meta(), etc
  12. WHAT DOES IT GIVE US? • One liners • $keys

    are automatically observed and the string recomputed on changes • Returns a computed property, so you can chain.readOnly(), .meta(), etc • Also...
  13. INLINE EXPRESSIONS • Accepts any valid JavaScript expression. • Identifiers

    are still looked up on context (not scope) so no need to use `this.key`
  14. WHAT ELSE? • Resolves properties on global context (e.g. window)

    if not found on current, otherwise replaces with empty string.
  15. WHAT ELSE? • Resolves properties on global context (e.g. window)

    if not found on current, otherwise replaces with empty string. • Supports variable variables. (But please don’t...)
  16. WHAT ELSE? • Resolves properties on global context (e.g. window)

    if not found on current, otherwise replaces with empty string. • Supports variable variables. (But please don’t...) • Customize the token (use # or whatever you want)
  17. WHAT ELSE? • Resolves properties on global context (e.g. window)

    if not found on current, otherwise replaces with empty string. • Supports variable variables. (But please don’t...) • Customize the token (use # or whatever you want) • Actually an Ember.js wrapper around my generic interpolation library, “String.interpolate.js”
  18. SECURITY CONSIDERATIONS • NEVER call .interpolate() directly on un-safe strings.

    • A malicious user could use ${expression} for XSS attacks
  19. SECURITY CONSIDERATIONS • NEVER call .interpolate() directly on un-safe strings.

    • A malicious user could use ${expression} for XSS attacks • Un-safe means you don’t have 100% control over it. Usually, that means user-generated.