These are chat archives for canjs/canjs

9th
Apr 2018
Eben
@eben-roux
Apr 09 2018 04:41
@justinbmeyer : I'll put together a jsbin example just to ensure I'm not doing something odd. I want to use a method on my view model within a component that is rendered in an {{#each}}. I therefore need to get to the scope.root.mymethod as I understand. The component has to call the method that is bound by rendering the on:click to the method: something like:
{{#each(subObjects)}}
   <my-component click:from="scope.root.the-method-on-the-main-viewmodel" />
{{/each}}
Justin Meyer
@justinbmeyer
Apr 09 2018 15:43
@eben-roux , so you are setting click on my-component's VM to that method
I think there might be a bug because I think that methods are supposed to be auto bound
Eben
@eben-roux
Apr 09 2018 15:44
@justinbmeyer I think I missed the whole function binding boat :) --- having a look now on a jsbin
Justin Meyer
@justinbmeyer
Apr 09 2018 15:44
I would call the click property something else. It's a bit confusing. Maybe clickCallback
This should work though:
<my-comp clickCallback:from="scope.root.method.bind(scope.root)">
it's possible that scope.root.X isn't being auto-bound. This happens in can-key-tree cc @phillipskevin
so, my bad... just wasn't using it correctly by the looks of it
Kevin Phillips
@phillipskevin
Apr 09 2018 16:01
not sure if you cleared this up correctly, but scope.root.X should get called with the correct context automatically
if you find a case where it isn't, it's a bug for sure
Eben
@eben-roux
Apr 09 2018 16:03
wrapping one's head around all these levels of binding takes some practice --- it'll probably get easier with time
Justin Meyer
@justinbmeyer
Apr 09 2018 16:04
can-stache-bindings_key.png
columns are:
binding example | direction of binding | value in the scope | pseudo code of what binding sets up
that's from this presentation: https://drive.google.com/open?id=0Bx-kNqf-wxZeYUJ3ZVRxUlU2MjQ .... it hasn't been updated for 4.0, but still covers the 3.0 bindings
Dovid Bleier
@dbleier
Apr 09 2018 18:27
Hi All, having some issues with the new can4.0 value and using its listenTo method. In my test cases, works perfectly but in the actual app, the listenTo callback isn't getting called, although interestingly enough, the value method itself is
export const ViewModel = DefineMap.extend({
    units: {
        default: () => { return []; },
    },
    users: {
        value: function(prop) {
            prop.resolve({});
            prop.listenTo(prop.lastSet, prop.resolve);
            prop.listenTo('units', (target, newval, oldval) => {
                let organizeUnits = (users, user, sign, unit) => {
                        users[user] = users[user] || {};
                        users[user][sign] = users[user][sign] || [];
                        users[user][sign].push(unit);
                        return users;
                    },
                    users = newval.reduce((users, unit) => {
                        return organizeUnits(users, unit.user || 'unassigned',
                            unit.currentSign ? unit.currentSign.path : 'unassigned', unit.serialize());
                    }, {});
                prop.resolve(users);
            });
        }
    },
units is set by an outside component via stache scope bindings
so each time units is updated the users.value method is called, but the prop.listenTo('units', ...) is not
I could just move the code right into the value method, but that doesn't seem right