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

calling methods directly on an instance of a can.Map with can.compute.read #2199

Closed
justinbmeyer opened this Issue Jan 21, 2016 · 2 comments

Comments

Projects
None yet
3 participants
@justinbmeyer
Contributor

justinbmeyer commented Jan 21, 2016

this doesn't work, it only reads functions on the prototype.

@justinbmeyer justinbmeyer added the bug label Jan 21, 2016

@justinbmeyer justinbmeyer added this to the 2.3.11 milestone Jan 21, 2016

@dylanrtt

This comment has been minimized.

Show comment
Hide comment
@dylanrtt

dylanrtt Jan 21, 2016

Contributor

My use case for this was creating a mixin for bit-tabs that binds the active tab to the route's tabId.

function bindTabsToRoute(tabsVM) {
    let oldMakeActive = tabsVM.makeActive,
        panels = tabsVM.attr('panels'),
        routeChangeHandler = function(ev, newVal, oldVal) {
            panels.each(panel => {
                if (panel.attr('tabId') === newVal) {
                    tabsVM.makeActive(panel);
                }
            });
        };

    // override makeActive() to also update the route
    tabsVM.makeActive = (panelVM) => {
        can.route.attr('tabId', panelVM.attr('tabId'));
        oldMakeActive.call(tabsVM, panelVM);
    };

    // bind to route
    can.route.bind('tabId', routeChangeHandler);

    // return teardown function
    return () => can.route.unbind('tabId', routeChangeHandler);
}

can.view.attr('bit-tabs-route', function(el, attrData) {
    let teardown = bindTabsToRoute(can.viewModel(el));
    can.one.call(el, 'removed', teardown);
});
Contributor

dylanrtt commented Jan 21, 2016

My use case for this was creating a mixin for bit-tabs that binds the active tab to the route's tabId.

function bindTabsToRoute(tabsVM) {
    let oldMakeActive = tabsVM.makeActive,
        panels = tabsVM.attr('panels'),
        routeChangeHandler = function(ev, newVal, oldVal) {
            panels.each(panel => {
                if (panel.attr('tabId') === newVal) {
                    tabsVM.makeActive(panel);
                }
            });
        };

    // override makeActive() to also update the route
    tabsVM.makeActive = (panelVM) => {
        can.route.attr('tabId', panelVM.attr('tabId'));
        oldMakeActive.call(tabsVM, panelVM);
    };

    // bind to route
    can.route.bind('tabId', routeChangeHandler);

    // return teardown function
    return () => can.route.unbind('tabId', routeChangeHandler);
}

can.view.attr('bit-tabs-route', function(el, attrData) {
    let teardown = bindTabsToRoute(can.viewModel(el));
    can.one.call(el, 'removed', teardown);
});

@daffl daffl modified the milestones: 2.3.12, 2.3.11 Jan 21, 2016

@justinbmeyer

This comment has been minimized.

Show comment
Hide comment
@justinbmeyer

justinbmeyer Jan 22, 2016

Contributor

I'm thinking of changing the logic to always call .attr(prop) and if that returns undefined, to return this[prop]. This will allow map._cid if someone wants it.

Contributor

justinbmeyer commented Jan 22, 2016

I'm thinking of changing the logic to always call .attr(prop) and if that returns undefined, to return this[prop]. This will allow map._cid if someone wants it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment