kellyselden / ember-awesome-macros Goto Github PK
View Code? Open in Web Editor NEWA collection of Ember computed macros
License: MIT License
A collection of Ember computed macros
License: MIT License
Would you be open to me making a PR for array.invoke
?
via http://emberjs.com/api/classes/Ember.Array.html#method_invoke
The initial value you provide is mutated and reused for each computation. It needs to be a fresh copy. @Turbo87 suggested to turn it into a factory function you supply that gives a fresh value every calculation.
ex:
import { promise } from 'ember-awesome-macros'; // promise.all
import { all } from 'ember-awesome-macros/promise';
import all from 'ember-awesome-macros/promise/all';
We can use the same method as math
, but should probably wait until #259 because of the bloat.
so people can make their own composable macros without every macro here.
Fails on this line, complaining Uncaught TypeError: array.sort is not a function
Indeed, Ember.ArrayProxy
doesn't seem to implement a sort
method (which I guess makes sense, since it's not entirely clear what it should do)
The official Ember.computed.sort()
does a slice()
to copy the array before sorting
I haven't delved into the ember-awesome-macros
code far enough to see where the parameter passed into ember-awesome-macros/array.sort()
originates, but it seems like doing the same slice()
-to-copy should fix this issue (and probably avoid others -- right now the original array is being mutated when the sort macro is accessed, which I certainly found unexpected)
test: computed('array.[]', function(array) {
return array;
})
it's the same as Ember.computed.or
After receiving the deprecation warning about htmlSafe
moving to string.htmlSafe
I tried to do the following: import { htmlSafe} from 'ember-awesome-macros/string'
but it is missing.
it looks like the string index does not export htmlSafe
. I would assume it should (as well as a few others that appear to be missing).
If @kellyselden agrees that this is an issue, I can implement the fix for this.
so you can do this:
test: length(filterBy('array', 'prop', true))
I was getting an import error for lt
and looked at the addon folder and saw that lt
was defined in lt-key.js
. I noticed most of comparison macros are named similarly.
Uncaught Error: Could not find module `ember-awesome-macros/lt` imported from `frontend/pods/components/appointments-list/component`
It's a bit more cryptic if you import in this way: import { lt } from 'ember-awesome-macros';
, because you get the error:
Uncaught TypeError: (0 , _emberAwesomeMacros.lt) is not a function
take from here
For example, if the array is undefined, do we return undefined or an empty array? Currently we return undefined, where lodash returns empty array. More discussion here #250 (comment).
otherwise it's really hard to figure out what the breaking changes are between each version... ๐ญ
two things:
A ollection(sic) of Ember computed macros
Instead of using one of these:
conditional(not('value'), raw('some text'))
conditional('value', raw(''), raw('some text'))
Would be nice to have something easier
When a key is undefined
, it is skipped when the macro is calculated. This has unexpected consequences when working with the logic macros (and, or, etc). For example, an and
of a true property and an undefined property will resolve as true.
now that emberjs/rfcs#176 is close to being ratified, now might be a good time to align the api here with the conventions in that RFC.
i only leave this comment because i saw on the readme that the api here is not yet stable / breaking changes are still possible.
It seems annoying that you have to import raw()
and computed()
from a different addon than ember-awesome-macros
itself. IMHO we should reexport those macros if we depend on them as our own public API.
context here mike-north/ember-lodash#86 (comment)
It would be great to be able to merge hash
computed properties from parent or mixins.
If possible I guess the best would be to make it compatible with by mergedProperties
.
https://emberjs.com/api/classes/Ember.Object.html#property_mergedProperties
Or something like
value1: hash({
prop1: 'source1',
prop2: hash({
prop: 'source2'
})
}),
value2: hash({
prop1: 'source3'
}).defaults('value1'),
So I have a CP like this:
areAllShorterThanFive_cp: Ember.computed('[email protected]', function () {
return this
.get('data')
.mapBy('name')
.every(name => name.length < 5)
}),
I've assumed it can be rewritten with ember-awesome-macros
like this:
areAllShorterThanFive_every_mapBy:
every(
mapBy('data', 'name'),
item => item.get('name').length < 5
),
But the latter does not recalculate. :(
Here's a demo: https://ember-twiddle.com/0dc43e01f156470d6c427f856963e0f4?openFiles=controllers.application.js%2C
I also tried this:
areAllShorterThanFive_every: every('[email protected]',
item => item.get('name').length < 5
),
but it produces a warning "You used the key "[email protected].[]" which is invalid." and still does not work.
We definitely need an everyBy
macro, but why doesn't every(mapBy())
work? Is there a way to make it work? Either it's a bug or I'm missing something important about the addon. I wonder how many issues like this I already have in my apps.
I am not sure whether this is intended or avoidable behavior, but in a scenario where you have the following:
condition: false,
property: conditional('condition', 'trueProp', 'falseProp'),
falseProp: computed(function() {
// we expect this to be called
return 'good'
}),
trueProp: computed(function() {
// but not this one
// this property could incorrectly assume that `condition` is true
// or it could be very slow
// etc
})
Essentially, the second argument to condition
could possibly not be executed until the first argument becomes true.
Thoughts?
I just recognized some of the macros use incomplete dependent keys. I looked at the array.filterBy
macro for example and realized that with this example:
model: Ember.A([
Ember.Object.create({ val: 1 }),
Ember.Object.create({ val: 2 })
]),
filterKey: 'val',
number2: filterBy('model', 'filterKey', 2)
the number2
property doesn't update when any of the model's objects' val
property changes (as the [email protected]
dependent key is missing from the CP).
I think in the case of macros like this that take only atomic arguments this should be fixable (might be much harder for macros like array.filter
where you'd have to inspect the passed function to get the element property that the CP needs to depend on). Having an incomplete implementation like this might lead to hard to track down bugs.
test: promiseObject(function() {
return this.store.findRecord('item');
})
should be
test: promiseObject(computed(function() {
return this.store.findRecord('item');
}))
no other macros have function syntax, we should use what computed already gives us.
Existing settable computed properties don't translate well to macros. Figure out how to handle. I'm thinking:
test: setable(and('test1', 'test2'))
will overwrite the property
test: setable(and('test1', 'test2'), {
set(key, value) {
set(this, 'test1', value);
}
})
to do something custom.
This is purely for backwards compatibility, and should not be used for new code as it is a bad practice.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.