Skip to content
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

Conversation

duggiefresh
Copy link
Contributor

Fixes #13296

@duggiefresh
Copy link
Contributor Author

Calling toString() here because of this assertion. Let me know if there's a better direction. Thanks!

@@ -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() {
Copy link
Member

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).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes! Thanks 💚

@duggiefresh duggiefresh force-pushed the 13296-allow-numeric-keys-for-get branch from 0ef6256 to a62d83c Compare April 12, 2016 15:44
@duggiefresh
Copy link
Contributor Author

Updated per review. I made an update to Glimmer util/references that will "stringify" numeric keys.

Thanks!

@@ -35,8 +35,10 @@ let DynamicKeyStream = BasicStream.extend({

key() {
let key = this.keyDep.getValue();
if (typeof key === 'string') {
if (typeof key === 'symbol') {
Copy link
Member

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?)

Copy link
Member

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 {
    // ...
  });
}

Copy link
Contributor Author

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

Copy link
Member

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
});

}

Copy link
Contributor Author

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'
    }
  });

°°°

Copy link
Member

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.

Copy link
Contributor Author

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

@duggiefresh
Copy link
Contributor Author

Sorry to bother, but just a ping to move this along.

Please see: #13311 (comment)

Thanks, @chancancode @rwjblue

@krisselden
Copy link
Contributor

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 null to 'null' for example.

@krisselden
Copy link
Contributor

Especially if this is targeted as a bugfix to beta.

@mixonic
Copy link
Member

mixonic commented Jun 26, 2016

@duggiefresh I think there was interest in removing the symbol specific code here? /cc @rwjblue

And @krisselden's suggestion re: swapping to a conservative approach, only coercing numbers, seems reasonable.

Would love to see this land :-)

@duggiefresh
Copy link
Contributor Author

@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

@duggiefresh duggiefresh force-pushed the 13296-allow-numeric-keys-for-get branch from ad368af to a7cb4ac Compare July 8, 2016 00:47
@duggiefresh
Copy link
Contributor Author

Updated per comments! I removed the symbol references and am now coercing number keys. Thanks 🍨

@duggiefresh
Copy link
Contributor Author

ping @mixonic @krisselden Thank you!

@Turbo87
Copy link
Member

Turbo87 commented Jul 13, 2016

does this work for 0 as a key too? I know I can use firstObject too, but in a index-loop it would be easier if 0 would work too.

@mixonic
Copy link
Member

mixonic commented Jul 13, 2016

@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;
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Turbo87
Copy link
Member

Turbo87 commented Jul 13, 2016

@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;
Copy link
Contributor

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?

Copy link
Contributor Author

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?

@homu
Copy link
Contributor

homu commented Aug 11, 2016

☔ The latest upstream changes (presumably #14041) made this pull request unmergeable. Please resolve the merge conflicts.

@locks
Copy link
Contributor

locks commented Nov 8, 2016

@duggiefresh are you able to rebase and solve the conflicts? Thank you!

@locks locks self-assigned this Apr 16, 2017
@tsteuwer
Copy link

Pease let this happen. I have to manually change keys to strings to use get with them 😊

@rwjblue
Copy link
Member

rwjblue commented Jun 16, 2017

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.

@rwjblue rwjblue closed this Jun 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.