Ember-CPM
Computed Property Macros for Ember
Requirements
Version 2.0+ will only work with Ember 2.0+ Version 3.0+ is only tested in the last 2 LTS.
Installation
Just run ember install ember-cpm
Usage
Just import individual macros from ember-cpm/macros/*
or all macros from ember-cpm
.
// Import only one macros;// or alternatively import all the namespace;
Contributing
To generate a new computed property macros with ember-cli
- Run
ember g macro <dasherized-macro-name>
. This will generate a few files./addon/macros/dasherized-macro-name.js
(the macro)./addon/tests/dummy/unit/macro/dasherized-macro-name-test.js
(a test)*- and modify
./addon/ember-cpm.js
// import the macro... var Macros = ... // allows use via EmberCPM.Macros.camelizedMacroName camelizedMacroName: camelizedMacroName ...;
ember d macro <dasherized-macro-name>
will do the reverse of these changes
Provided Macros
among
-- returnstrue
if the original value is among the given literals (testing using===
)encodeURIComponent
-- callsencodeURIComponent
on the original valueencodeURI
-- callsencodeURI
on the original valuefirstPresent
-- returns the first property with a value, as determined byEmber.isPresent
fmt
-- pass the original values into a format-stringhtmlEscape
-- escapes the original value withHandlebars.Utils.escapeExpression
and wraps the result in aHandlebars.SafeString
(since it's now safe)ifNull
-- fall back on a default valuepromise
-- wraps the original value in an already-resolved promisesafeString
-- wraps the original value in aHandlebars.SafeString
join
-- joins the supplied values together with a provided sepatatorquotient
-- divides one numeric property or literal by anotherdifference
-- subtracts one numeric property or literal from anotherproduct
-- multiplies numeric properties and literals togethersum
-- sums numeric properties and literals togetherconditional
-- returns values based on a boolean property (good replacement for ternary operator)computedPromise
-- Updates computed property when supplied callback (which must return a promise) is resolved
Composable Computed Property Macros
Unlike Ember's computed property macros, the macros in this addon are composable. That means you define macros inside other macros without defining them in a separate key.
;; const Macros: sum difference product = EmberCPM; Component;