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

Wires crossed with CachedQuery data objects #144

Closed
dustinfarris opened this issue Dec 4, 2016 · 3 comments
Closed

Wires crossed with CachedQuery data objects #144

dustinfarris opened this issue Dec 4, 2016 · 3 comments

Comments

@dustinfarris
Copy link
Contributor

I am running into a strange scenario where the data in a cached query looks like (but is oddly not an instance of) Ember.Object. It has getters and setters and meta properties just like an Ember.Object. I am noticing this specifically in the mutation handler currentResponse param:

CachedQuery objects are internal to cashay so I am astonished that I am seeing something that looks like Ember.Object.

I am investigating where this overlap is occurring. I assume the problem is on my end, but I noticed this line in the action project which made me think there might be a larger issue here.

@mattkrick
Copy link
Owner

mattkrick commented Dec 5, 2016 via email

@dustinfarris
Copy link
Contributor Author

dustinfarris commented Dec 5, 2016

I think I figured this out.

In my stateToComputed (our version of stateToProps in react-redux), I do this:

stateToComputed = (state, { projectId }) => {
  const { data: { project } } = cashay.query(projectQuery, { variables: { projectId } });
  return { project };
}

With project now available as a computed property, I can use it in the template:

{{project-detail project=project}}

As soon as I pass it as an attr to another component like this, Ember "binds" it which involves transforming the project object into one that signals Ember whenever it changes. It does this by adding meta data, adding getters and setters—basically hi-jacking the POJO. This affects redux state! It is also why Object.assign doesn't work.

It seems the only way around this is to deep copy the data before returning it in stateToComputed, e.g.:

  const { data: { project } } = cashay.query(projectQuery, { variables: { projectId } });
  return {
    project: jQuery.extend(true, {}, project)
  };

I am going to open an issue in ember-redux to address this.

Update: see ember-redux/ember-redux#58

@mattkrick
Copy link
Owner

oh wow, i didn't know ember worked like that! yeah, the redux state should be immutable, so if ember is going to try & mutate it, you'll wanna put something between the redux state & the view layer, otherwise you'll end up having to deep copy an object on every re-render which will get expensive.

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

No branches or pull requests

2 participants