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

getWithDefault broken with chained properties #13444

Closed
jamesarosen opened this issue May 4, 2016 · 13 comments
Closed

getWithDefault broken with chained properties #13444

jamesarosen opened this issue May 4, 2016 · 13 comments
Labels

Comments

@jamesarosen
Copy link

jamesarosen commented May 4, 2016

Let's explore how get(...) || y and getWithDefault(x, y) compare.

> o = Ember.Object.create()
> o.get('foo') || 'x'
"x"
> o.getWithDefault('foo', 'x')
"x"

👍 OK, that sounds totally right.

> o.set('foo', 'bar')
> o.get('foo') || 'x'
"bar"
> o.getWithDefault('foo', 'x')
"bar"

👍 Cool. Makes sense.

> o = Ember.Object.extend({ foo: null }).create()
> o.get('foo') || 'x'
"x"
> o.getWithDefault('foo', 'x')
null

😕 Hmm. That obeys the API docs but it doesn't seem super intuitive. Others agree with me, but there's also good reason to keep it as-is. In our app, we use foo: null as an indication that the client should pass foo in; maybe we should change that to foo: undefined. Oh well, let's press on.

> o = Ember.Object.create()
> o.get('foo.bar')
undefined
> o.getWithDefault('foo.bar', 'x')
"x"

👍 Diggin' it.

> o = Ember.Object.extend({ foo: null }).create()
> o.get('foo.bar') || 'x'
"x"
> o.getWithDefault('foo.bar', 'x')
null

WHAT!?!?!?!??!!? :trollface:

@mmun
Copy link
Member

mmun commented May 4, 2016

This is a bug in Ember.get:

Ember.get({ foo: null }, 'foo.bar') // null

It should be undefined.

@jamesarosen
Copy link
Author

@mmun on Ember 1.13, I get

> Ember.get({ foo: null }, 'bar')
undefined

which is correct as far as I can tell.

@mmun
Copy link
Member

mmun commented May 4, 2016

Oops. I was checking using the console on http://discuss.emberjs.com/ but it seems to be running 1.12 :P

@mmun
Copy link
Member

mmun commented May 4, 2016

You want foo.bar there, btw.

@mmun
Copy link
Member

mmun commented May 4, 2016

I just checked it against canary and Ember.get({ foo: null }, 'foo.bar') === null still.

@jamesarosen
Copy link
Author

You want foo.bar there, btw.

Ah!

@Serabe Serabe added the Bug label May 4, 2016
@pittst3r
Copy link
Contributor

pittst3r commented May 4, 2016

I have a failing test for this. I'll start a WIP PR.

@jamesarosen
Copy link
Author

Here is the bad line as far as I can tell. My guess is that it should be something more like

if (obj == null) {
  return i === parts.length - 1 ? obj : undefined;
}

@locks
Copy link
Contributor

locks commented May 4, 2016

related: #13449

@pittst3r
Copy link
Contributor

pittst3r commented May 4, 2016

@jamesarosen Yeah, that's where I ended up as well. This was my solution:

if (obj == null) {
  if (i < parts.length) {
    return undefined;
  }
  return obj;
}

@pittst3r
Copy link
Contributor

pittst3r commented May 4, 2016

I haven't committed it yet though because I'm not convinced it's the best solution. Going to keep tinkering with this.

@pittst3r
Copy link
Contributor

pittst3r commented May 5, 2016

Tried a recursive approach for _getPath but it didn't turn out to be the degree of improvement I had hoped for so I'm sticking with what's there already, but with the above fix. The PR is ready unless anyone has another suggestion. 💃

@pittst3r
Copy link
Contributor

pittst3r commented May 6, 2016

@jamesarosen Looks like the fix will be in the next beta.

This issue can be closed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants