These are chat archives for canjs/canjs

14th
Aug 2015
Matthew Phillips
@matthewp
Aug 14 2015 12:28
that's because you've overwritten parent.prototype.define
whoops, i just responded to a comment from a few days ago
carry on
Justin Meyer
@justinbmeyer
Aug 14 2015 20:20

@ilyavf asked:

3:18 PM
Do you know why this setter is called twice after a template binding is added? http://jsbin.com/sinunew/edit?js,console,output

Ilya Fadeev
@ilyavf
Aug 14 2015 20:21
Yeah, thanks @justinbmeyer . Click update to see it. If you remove the binding declaration from the template, the setter is called once.
Justin Meyer
@justinbmeyer
Aug 14 2015 20:21
@ilyavf so is the button or anything related to this?
like, does there even have to be a component?
to see the problem
could you just pass a map with similar defined behavior?
Ilya Fadeev
@ilyavf
Aug 14 2015 20:22
ok, will do
like, with a bind() ?
Justin Meyer
@justinbmeyer
Aug 14 2015 20:22
oh, I have to click update to see the problem?
Ilya Fadeev
@ilyavf
Aug 14 2015 20:22
yes, u have to click
Justin Meyer
@justinbmeyer
Aug 14 2015 20:24
this is most likely b/c of the two-way binding
clicking update changes child's searchTerms, which changes parent's myval, which again tries to change child's searchTerms
oh, those only track one direction
we might need to add the other direction
viewModelPropertyUpdates[searchTerms] will be true only after parent's myval changes
we could have a similar protection coming back the other way
Justin Meyer
@justinbmeyer
Aug 14 2015 20:29
basically when parent CHANGES child ... we protect against child then changing the parent
but when child CHANGES parent ... we don't currently protect the parent from trying to change the child as a result
Ilya Fadeev
@ilyavf
Aug 14 2015 20:30
what line would this be ("when child changes parent")?
Justin Meyer
@justinbmeyer
Aug 14 2015 20:30
this is something for @daffl to be aware of
that's the code that runs when a child's viewModel is changed
easier to read in minor
b/c variables are properly called viewModel there
that code is what listens to the child viewModel and sets up two-way binding
well, the child -> parent binding
that code listens on the value passed to the component ... essentially setting up parent -> child binding
Ilya Fadeev
@ilyavf
Aug 14 2015 21:25
@justinbmeyer If I wrap line https://github.com/bitovi/canjs/blob/minor/component/component.js#L171 into an if statement (componentScope[name] !== newVal) this should fix it, right?
Justin Meyer
@justinbmeyer
Aug 14 2015 21:29
That would not be a good fix
  • it would not be correct ... it's possible that a setter or getter exists on this property, that what you read there wouldn't be accurate.
  • it's best to never access property values directly on can.maps
also, in reality ... can.Map already has internal checks for this
the setter is still called
but an event wont be produced
all we are doing here is making sure sets aren't called unnecessarily
btw, is this causing a real problem in your app?
if so ... that makes me think your setter has strange side-effects
Ilya Fadeev
@ilyavf
Aug 14 2015 21:32
i had to put a fix (manually check in my setter if current value is different from a prev one)
Justin Meyer
@justinbmeyer
Aug 14 2015 21:32
why?
if the setter returned the same value
no events would be triggered
Ilya Fadeev
@ilyavf
Aug 14 2015 21:33
it does have a side effect
Justin Meyer
@justinbmeyer
Aug 14 2015 21:33
why?
what is it doing?
Ilya Fadeev
@ilyavf
Aug 14 2015 21:33
so, there is an array of searchTerms that I want to filter grid data against
i guess, i should use a derived list listening to searchTerms
Justin Meyer
@justinbmeyer
Aug 14 2015 21:35
side-effectual setters aren't necessarily a bad thing, I've used them a lot .. like in the make-model-year example
however, those tend to be calling other local setters
so even if things are called multiple times, my code would not be effected
Ilya Fadeev
@ilyavf
Aug 14 2015 21:35
would it be a better pattern to use getters instead of a side-effect setters?
Justin Meyer
@justinbmeyer
Aug 14 2015 21:35
yes, I think so
imo, it's more clear
but sometimes you need a setter
but I think @daffl created an issue about the 'double set' problem in a component
the right fix would be to do something similar to what's already being done with viewModelPropertyUpdates
David Luecke
@daffl
Aug 14 2015 21:37
For what problem? I don't think I did?
Justin Meyer
@justinbmeyer
Aug 14 2015 21:37
the problem you just said you'll create an issue for
in hipchat
basically ... when a child changes it's parent ... wait until the next batch for a parent to be able to update that child property