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

[0.8] accidental shared state in configure value? #1269

Closed
IntranetFactory opened this issue Mar 8, 2015 · 5 comments
Closed

[0.8] accidental shared state in configure value? #1269

IntranetFactory opened this issue Mar 8, 2015 · 5 comments
Labels

Comments

@IntranetFactory
Copy link

I have an element with a property of type Object with a default value like

    properties: {
      user: {
        type: Object,
        notify: true,
        value: {
          name: {
            first: 'James',
            last: 'Cook',           
          },
          counter: 1
        }        
      }

When using that element all instances share the same "user" object.

I think the reason for this is
https://github.com/Polymer/polymer/blob/0.8-preview/src/features/standard/configure.html#L75

After I changed that line to

            // deep copy config value
            config[i] = (JSON.parse(JSON.stringify(c.value)));  

the shared state didn't happen any longer.

@ssorallen
Copy link
Contributor

If the initial value is a primitive like a Number, a Boolean, or a String, it is okay to set it as the value. If the initial value is an Object you need to provide a function that returns the initial:

value: function() {
  return {
    name: {
      first: 'James',
      last: 'Cook',           
    },
    counter: 1
  };
}

@ssorallen ssorallen added the 0.8 label Mar 9, 2015
@IntranetFactory
Copy link
Author

Wouldn't it make more sense to automatically also copy non primitive types if no function is provided? It would still be possible to write a function when a copy is not sufficient. Just creating an avoidable shared state seems to be not really convenient.

@ssorallen
Copy link
Contributor

A shallow copy works in the case you gave, but consider more complex data structures that are not just simple JavaScript objects. This is a case better left to the developer because he or she has more knowledge about the data structure than the framework can possibly infer.

@IntranetFactory
Copy link
Author

I understand the idea - but I didn't want to remove the function. I just think it's quite surprising that when no function is provided a shared state is created. If using a default value for an object really requires to always write a function then without such a function, I think no default should be copied.

@sorvell
Copy link
Contributor

sorvell commented May 22, 2015

This can be a little confusing, but it's also fundamental language functionality. We can't know what's appropriate since there are cases when it's desirable for the object to actually be shared on the prototype. For these reasons, we've elected to allow the user to give Polymer explicit information by providing a function that can generate the default property value.

@sorvell sorvell closed this as completed May 22, 2015
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

3 participants