These are chat archives for canjs/canjs

5th
May 2017
Gregg Roemhildt
@roemhildtg
May 05 2017 18:47 UTC
Question about can-components, I think it'd be useful to pass an object to a viewmodel, rather than as individual properties: <my--component {%viewModel}="options" /> so then viewModel would be passed options to its constructor. Is this currently possible?
Maybe I could do something like this: <my-component ($inserted)="init(options)" /> hey, that might work
Morgan Heimbeck
@Xitstrategies
May 05 2017 18:55 UTC
@roemhildtg you can pass an object. {options}="options"
Gregg Roemhildt
@roemhildtg
May 05 2017 18:55 UTC
Yes, but that would set the property on the view model. I want each property in options to be set on the viewmodel
so if I have options like { prop: 'value'} then the result would be viewModel.prop // 'value'
Morgan Heimbeck
@Xitstrategies
May 05 2017 18:57 UTC
I get it. I thought they had that, looking for something in their docs
Brad Momberger
@bmomberger-bitovi
May 05 2017 18:57 UTC
The viewModel can be exported to the reference scope, but there isn't a way that I'm aware of to set the viewModel as a whole with an object.
However, you can probably roll your own with can-view-callbacks.attr
Gregg Roemhildt
@roemhildtg
May 05 2017 18:58 UTC
I was wondering if that could work
Brad Momberger
@bmomberger-bitovi
May 05 2017 19:03 UTC
something like:
callbacks.attr("options", function(el, attrData) {
   var scopeRead = el.getAttribute(attrData.attributeName);
   assign(canViewModel(el), attrData.scope.get(scopeRead));
 });
Gregg Roemhildt
@roemhildtg
May 05 2017 19:04 UTC
Cool. I'll give that a shot
Thanks for the suggestion
Gregg Roemhildt
@roemhildtg
May 05 2017 19:09 UTC
:thumbsup: works perfectly!
Brad Momberger
@bmomberger-bitovi
May 05 2017 19:10 UTC
That's amazing. I didn't test that at all.
Gregg Roemhildt
@roemhildtg
May 05 2017 19:10 UTC
I literally copied and pasted
Gregg Roemhildt
@roemhildtg
May 05 2017 19:16 UTC
So basically, I can insert a component, like this:
document.body.appendChild(stache('<my-component options="options" />')({ options: options}));
and I don't have to worry about changing my template if I add or remove properties
Brad Momberger
@bmomberger-bitovi
May 05 2017 19:18 UTC
Yeah. There isn't any live binding happening in the callback (there could be, as the attrData comes with a compute, but we're not using it in the example) so it shouldn't fire more than once, when the stache is hydrated.