Git Product home page Git Product logo

ember-computed-template-string's Introduction

ember-computed-template-string

Build Status Ember Observer Score

Consider the canonical computed property example:

Ember.Object.extend({
  firstName: 'Serena',
  lastName: 'Fritsch',

  fullName: Ember.computed('firstName', 'lastName', function() {
    return `${this.get('firstName')} ${this.get('lastName')}`;
  })
});

ember-computed-template-string provides a macro which removes the duplication:

import templateString from 'ember-computed-template-string';

Ember.Object.extend({
  firstName: 'Serena',
  lastName: 'Fritsch',

  fullName: templateString("${firstName} ${lastName}")
});

Installation

This is an Ember CLI addon, to install:

ember install ember-computed-template-string

Development Instructions

  • git clone this repository
  • npm install
  • bower install

Running

ember-computed-template-string's People

Contributors

dependabot[bot] avatar ember-tomster avatar enrico-intercom avatar gavinjoyce avatar jasonmit avatar lolmaus avatar patocallaghan avatar serenaf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ember-computed-template-string's Issues

Consider using RegExp and CPM instead of a Babel transform

I recently watched (and enjoyed!) @serenaf's talk about this addon at EmberCamp London 2016.

I had an idea for how this might be doable without having to resort to a Babel transform (which I had a few concerns about) while still maintaining the same interface. I gave it a shot and was able to get this to work using a computed property macro and a RegExp.

The idea behind my approach relies on three observations:

The approach is then to create a computed property macro which:

  • Constructs and uses a regular expression to get all properties/paths in the passed template string, then
  • Uses those properties (after a uniq to avoid duplication) as the dependent keys of the generated computed property, and finally
  • Uses String.prototype.replace to replace instances of ${property} with the get() lookup of those properties.

You can see my solution in a twiddle (source). For the tests I copied the tests in this project's tests/unit/custom-replace-call-paths-test.js, and they seem to all pass.

Cannot read property 'replaceCallPaths' of undefined

Cannot read property 'replaceCallPaths' of undefined
TypeError: Cannot read property 'replaceCallPaths' of undefined
    at ComputedTemplateStringTransform (/home/lolmaus/Code/launchpad2/node_modules/ember-computed-template-string/plugins/computed-template-string-transform.js:5:43)
    at CoreObject.module.exports.included (/home/lolmaus/Code/launchpad2/node_modules/ember-computed-template-string/index.js:16:38)
    at CoreObject.superWrapper [as included] (/home/lolmaus/Code/launchpad2/node_modules/core-object/lib/assign-properties.js:32:18)
    at EmberApp.<anonymous> (/home/lolmaus/Code/launchpad2/node_modules/ember-cli/lib/broccoli/ember-app.js:468:15)
    at Array.filter (native)
    at EmberApp._notifyAddonIncluded (/home/lolmaus/Code/launchpad2/node_modules/ember-cli/lib/broccoli/ember-app.js:463:45)
    at new EmberApp (/home/lolmaus/Code/launchpad2/node_modules/ember-cli/lib/broccoli/ember-app.js:138:8)
    at module.exports (/home/lolmaus/Code/launchpad2/ember-cli-build.js:8:15)
    at CoreObject.module.exports.Task.extend.setupBroccoliBuilder (/home/lolmaus/Code/launchpad2/node_modules/ember-cli/lib/models/builder.js:74:19)
    at CoreObject.module.exports.Task.extend.init (/home/lolmaus/Code/launchpad2/node_modules/ember-cli/lib/models/builder.js:54:10)

Process finished with exit code 1

Immediate issue is here.

The problem is that ComputedTemplateStringTransform function is called with app.options.emberComputedTemplateString, but emberComputedTemplateString property is never set and thus is undefined.

Warn on invalid templates

We should warn on uses likes this:

  • Computed.templateString('${adminName}') - use Computed.reads or Computed.alias
  • Computed.templateString('this has no dynamic section')

Alternative variable interpolation syntax

Hey there,

what do you think about using {{variable}} as variable interpolation syntax? So that instead of writing

fullName: templateString("${firstName} ${lastName}")

You would write instead

fullName: templateString("{{firstName}} {{lastName}}")

The motivation for that is that the addon is called ember-computed-template-string and this would be more consistent with handlebars variable interpolation, while using the ${variable} syntax looks much more like ES6 variable interpolation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.