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

2-Way-Binding for Polymer Properties #2144

Open
jogibear9988 opened this issue Oct 28, 2016 · 1 comment
Open

2-Way-Binding for Polymer Properties #2144

jogibear9988 opened this issue Oct 28, 2016 · 1 comment
Milestone

Comments

@jogibear9988
Copy link

With this Code you can 2-way-bind to Polymer Properties:

        ko.bindingHandlers['polymer'] = {
            'init': function (element, valueAccessor, allBindings) {
                var eventsToHandle = valueAccessor() || {};
                ko.utils.objectForEach(eventsToHandle, function (eventName, value) {
                    if (typeof eventName == "string") {
                        var polymerEvent = eventName.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase() + '-changed';
                        var listener = function(e) {
                            value(e.detail.value);
                        };
                        element.addEventListener(polymerEvent, listener);
                        ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
                            element.removeEventListener(polymerEvent, listener);
                        });
                    }
                });
            },
            'update': function (element, valueAccessor, allBindings) {
                var value = ko.utils.unwrapObservable(valueAccessor()) || {};
                ko.utils.objectForEach(value, function (attrName, attrValue) {
                    attrValue = ko.utils.unwrapObservable(attrValue);
                    element[attrName] = attrValue;
                });
            }
        };
@mbest mbest modified the milestone: Not assigned Dec 2, 2016
@jogibear9988
Copy link
Author

IMPL IN #2176

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