These are chat archives for canjs/canjs

21st
Mar 2018
Andrey Furs
@yganda
Mar 21 2018 12:30
Hi guys, could anyone help me? I use construction like this: MyModel.getList({
id: this.myPerson.id
})
.then((data) => data.filter(item => item.id === this.myPerson.id2)), so the problem is - data.lenght is 0, but after 2 seconds data is normal. Maybe somebody knows why it happens? I use 3rd canjs
Kevin Phillips
@phillipskevin
Mar 21 2018 14:23
@yganda if you're using can-connect it will load data from the cache first
and then will update the instances with the data that comes back from the server
you should not filter your data like that in the then of the promise
because that will only run with the cached data, and not with the server data
instead you should create another property that is derived from that one
Andrey Furs
@yganda
Mar 21 2018 14:25
@phillipskevin thanks a lot!
Kevin Phillips
@phillipskevin
Mar 21 2018 14:25
no problem
Gregg Roemhildt
@roemhildtg
Mar 21 2018 16:50

Quick question: I have a component view model, that when a particular property gets set, I want to do other things. So what I need to happen is this.propneeds to be updated, then I need to perform some other task that uses the updated this.prop

Basic example ( I know this won't work as written)

prop: {
set(prop){
   this.prop = prop;
   doOtherStuff();
}
},
doOtherStuff(){
   // do stuff with updated this.prop
}
Kevin Phillips
@phillipskevin
Mar 21 2018 17:38
what is the other stuff?
if it is updating other properties, you should make those properties depend on prop using getters or the new value behavior
if you need to do something with the DOM, you can use https://canjs.com/doc/can-component/connectedCallback.html
Gregg Roemhildt
@roemhildtg
Mar 21 2018 17:43
getters - yeah, that's what I started working on after I thought about it a bit.
Kevin Phillips
@phillipskevin
Mar 21 2018 17:44
https://canjs.com/doc/guides/forms.html#Resettingaselectionwhenitsparentlistchanges has an example using value for something that is more complicated than a getter can handle
Gregg Roemhildt
@roemhildtg
Mar 21 2018 17:53
I think I can handle this scenario with a getter...the connected callback is probably what I need. Would that be a good place to put something like this?
    // workLayer is a property resolved in async getter
    this.on('workLayer', (evt, newLayer, oldLayer) => {
      if (oldLayer) {
        this.view.map.remove(oldLayer);
      }
      if (newLayer) {
        this.view.map.add(newLayer);
      }
    });
Gregg Roemhildt
@roemhildtg
Mar 21 2018 17:59
I think this is working. To avoid memory leaks, is there something else I should add?
Kevin Phillips
@phillipskevin
Mar 21 2018 18:06
yeah, you should return a function for disconnecting
you can use what's in the docs
return this.stopListening.bind( this );
Gregg Roemhildt
@roemhildtg
Mar 21 2018 18:33
Ah, of course. Thanks!
Marc Godard
@MarcGodard
Mar 21 2018 20:55
Hey bitovi, canjs4 is great, migrated one of my project apps (thanks to @ilyavf for updating grid-component) and everything works amazing, and I feel like it loads faster and just works better. Great job... also the code seems cleaner somehow. Thanks guys.