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.0-rc.5] observers as an array #1527

Closed
jasonpecor opened this issue May 13, 2015 · 4 comments
Closed

[0.8.0-rc.5] observers as an array #1527

jasonpecor opened this issue May 13, 2015 · 4 comments

Comments

@jasonpecor
Copy link

Property observers work when custom element is instantiated, but never after that. No errors in console. Here is the javascript from the custom element definition used in my test:

var ChatUser = Polymer({
    is: "chat-user",
    properties: {
        user: Object
    },
    constructor: function (user) {
        this.user = user;
    },  
    observers: [
        "userStatusChanged(user.status)"
    ],  
    userStatusChanged: function (status) {
        console.log("updating status", status);
        this.notifyPath("user.status", status);
    }
});

Testing the custom element's observer:

var userData = {username: "Bob", status: "Offline"};
var user = new ChatUser(userData); // console logs "updating status Offline"
// change status
userData.status = "Online"; // observer ignores this
@jasonpecor jasonpecor changed the title observers as an array (0.8.0-rc.5) [0.8.0-rc.5] observers as an array May 13, 2015
@arthurevans
Copy link

You need to use the setPathValue method to update the property (or update it using
data binding). See:

https://www.polymer-project.org/0.8/docs/devguide/data-binding.html#path-binding

In this case, you're updating the status from outside, I think instead of:

userData.status = "Online";

In your example, you'd do something like:

user.setPathValue("user.status", "Online");

(Note: in 0.9, setPathValue is changing its name to just set).

@jasonpecor
Copy link
Author

So I have to explicitly call a setter on every custom element using that object. I was under the impression that using the observers array would set up Object.observe. I was hoping I could have more than one element observe a single object, so that changes to that one object would essentially be broadcast to all the observing elements, updating their views.

A use case along the lines of this example would be a chat room with a user list of ChatUser elements, and a chat window of ChatMessage elements. If "Bob" were to change his nickname to "Robert", updating his userData object would change his nickname both in the user list, and on any chat messages he's posted in the chat window.

@arthurevans
Copy link

0.8/0.9 does not use Object.observe. This was one of the performance changes -- As I understand it, O.o and its polyfill were both quite unpredictable from a performance standpoint.

If you use data binding to pass values, you can get many of the same benefits, but how exactly you'd do that may depend on how your app is set up. I recommend putting together a code sample and either posting a question on Stack Overflow or joining the Polymer slack channel (bit.ly/polymerslack)... There are a lot of people on there who've been working with 0.8/0.9 and may have suggestions.

@jasonpecor
Copy link
Author

Thank you very much for your help. I'll do as you've suggested.

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