-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUGFIX beta] Allow numeric keys for the get
keyword
#13311
[BUGFIX beta] Allow numeric keys for the get
keyword
#13311
Conversation
Calling |
@@ -471,6 +471,24 @@ QUnit.test('a numeric key can be used with {{each}}', function() { | |||
equal(view.$().text(), '123'); | |||
}); | |||
|
|||
QUnit.test('numeric keys work with the `get` keyword', function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test should be added in https://github.com/emberjs/ember.js/blob/master/packages/ember-glimmer/tests/integration/helpers/get-test.js (using that new format). That file is symlinked to ember-htmlbars tests so that it runs under both glimmer and htmlbars rendering engines.
See #13127 for a detailed description of that test format (and the I-N-U-R test cycle we are using in the ember-glimmer test suite).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes! Thanks 💚
0ef6256
to
a62d83c
Compare
Updated per review. I made an update to Glimmer util/references that will "stringify" numeric keys. Thanks! |
a62d83c
to
ad368af
Compare
@@ -35,8 +35,10 @@ let DynamicKeyStream = BasicStream.extend({ | |||
|
|||
key() { | |||
let key = this.keyDep.getValue(); | |||
if (typeof key === 'string') { | |||
if (typeof key === 'symbol') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test for this? (If the browser supports it?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (supportsNativeSymbols) {
moduleFor('Helpers test: {{get}} with symbols', class extends RenderingTest {
// ...
});
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can do! Is there an example somewhere of how to check for browser support within tests? Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think something like what I mentioned above should work:
moduleFor('Helpers test: {{get}}', class extends RenderingTest {
// current tests
}
if (typeof Symbol === 'function') { // I think?
moduleFor('Helpers test: {{get}} with symbols', class extends RenderingTest {
// moar tests
});
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the help. However, it seems there isn't support for symbols in templates. Do we want to add symbol support for the {{get}}
keyword?
For example, something like this doesn't work either:
['@test should be able to get an object value with numeric keys']() {
let first = Symbol('first');
let second = Symbol('second');
let third = Symbol('third');
this.render(`{{#each indexes as |index|}}[{{get items index}}]{{/each}}`, {
indexes: [first, second, third],
items: {
[first]: 'First',
[second]: 'Second',
[third]: 'Third'
}
});
°°°
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, if we don't support symbols already in template-land, then we don't need to support it explicitly here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! Thanks will update the PR as necessary
Sorry to bother, but just a ping to move this along. Please see: #13311 (comment) Thanks, @chancancode @rwjblue |
I don't understand why we don't check for number and coerce it? Can we start conservatively? Does it make sense to toString anything? This will toString |
Especially if this is targeted as a bugfix to beta. |
@duggiefresh I think there was interest in removing the And @krisselden's suggestion re: swapping to a conservative approach, only coercing numbers, seems reasonable. Would love to see this land :-) |
@mixonic Thanks for pinging me on this. I believe I'll be attending your Contributors Workshop at WGE. Perhaps, we can land this before the conference. :) See you then |
ad368af
to
a7cb4ac
Compare
Updated per comments! I removed the |
ping @mixonic @krisselden Thank you! |
does this work for |
@Turbo87 I'm not quite sure what you're asking. Can you provide an example? |
@@ -127,6 +127,11 @@ export class PropertyReference extends CachedReference { // jshint ignore:line | |||
} | |||
|
|||
get(propertyKey) { | |||
if (typeof propertyKey === 'number') { | |||
// Allows us to get numeric keys | |||
propertyKey = '' + propertyKey; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kind of expect you would only need the logic in the keyword. What other use-cases does this cover?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the coercion here too, because of the assertion here: https://github.com/emberjs/ember.js/blob/master/packages/ember-metal/lib/property_get.js#L49
@mixonic I'm talking about something like {{#each someArray as |value index|}}
value: {{get some.other.array index}}
{{/each}} |
@@ -37,6 +37,8 @@ const DynamicKeyStream = BasicStream.extend({ | |||
let key = this.keyDep.getValue(); | |||
if (typeof key === 'string') { | |||
return key; | |||
} else if (typeof key === 'number') { | |||
return '' + key; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you explain why it is needed to be check in both places?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a coercion in both ember-glimmer
and ember-htmlbars
because of the assertion here: https://github.com/emberjs/ember.js/blob/master/packages/ember-metal/lib/property_get.js#L49
Perhaps there's a better place to add this logic?
☔ The latest upstream changes (presumably #14041) made this pull request unmergeable. Please resolve the merge conflicts. |
@duggiefresh are you able to rebase and solve the conflicts? Thank you! |
Pease let this happen. I have to manually change keys to strings to use get with them 😊 |
I rebased and updated to work on master, but this PR predates github's feature that allows project maintainers to push to PR branches. I resubmitted (with correct commit attribution) in #15366. |
Fixes #13296