These are chat archives for canjs/canjs

4th
Jun 2018
Gregg Roemhildt
@roemhildtg
Jun 04 2018 13:23

Hey guys, so I defined a custom callback in stache using can-stache-callbacks like:

callbacks.attr('props', function(el, attrData) {
    const key = el.getAttribute('props');
    const data = attrData.scope.find(key);
    const viewModel = canViewModel(el);
    if (viewModel && data) {
        canReflect.assign(viewModel, data);
    } else {
        // dev-remove-start
        // eslint-disable-next-line
        console.warn('could not find viewmodel or data props for ' + key);
        // dev-remove-end
    }
});

but its running after the view model is initiated. Is it possible to set it up so that the props get passed like normal properties and the values will be available to the viewmodel immediately after being inserted?

Jeroen Cornelissen
@jeroencornelissen
Jun 04 2018 15:10
I’m seeing this error recently Uncaught TypeError: Cannot read property 'Symbol(can.getSchema)' of undefined at Object.getSchema (schema.js:110) Is this a known issue or is it something at our side :fire: ?
Jeroen Cornelissen
@jeroencornelissen
Jun 04 2018 19:36
Hmm, nvm. Something is pushing undefined in a list.
Chasen Le Hara
@chasenlehara
Jun 04 2018 20:08
@roemhildtg I’m having a hard time understanding what your goal is. 😅 Is it to have the VM not be initialized with the initial values? Or that you control when the props get set?
Gregg Roemhildt
@roemhildtg
Jun 04 2018 20:31
@chasenlehara My goal is simply to pass props to the component, but as an object. I guess its a temporary workaround until the stache implements the spread operator.
Chasen Le Hara
@chasenlehara
Jun 04 2018 20:59
Ahh gotcha @roemhildtg. One trick would be to do something like <my-comp this:from="vmObject" /> to set the this of my-comp to the vmObject
Justin Meyer
@justinbmeyer
Jun 04 2018 21:00
@chasenlehara does that work?
I don't think it does
Chasen Le Hara
@chasenlehara
Jun 04 2018 21:02
Mohamed Cherif Bouchelaghem
@cherifGsoul
Jun 04 2018 21:06
@roemhildtg what are you trying to acheive? getting a prop value in the scope?
RyanMilligan
@RyanMilligan
Jun 04 2018 21:14

Hi! We have a page (using Can 3) where the user can edit multiple objects in a list and then click Save to save all of them. Currently we loop through the list to find any items that are dirty and call save() on those, but that ends up firing a lot of separate requests to the middle tier which a) can't all be done in one transaction like we'd like them to be, and b) is sending potentially a lot of requests to our database at the same time and causing deadlocks, since they all edit the same table.

What I'd really like is a save function on List that serializes the objects inside an array, but the existing save pipeline seems very specific to passing single instances. Is there any pre-built solution to this, or would we have to craft our own request and figure out ourselves how to get the live objects updated (e.g., with final ids that come back from the server)?

Mohamed Cherif Bouchelaghem
@cherifGsoul
Jun 04 2018 21:24
@roemhildtg is this what you want to acheive? https://jsbin.com/facatur/edit?html,js,console,output cc @chasenlehara
Chasen Le Hara
@chasenlehara
Jun 04 2018 21:45
@RyanMilligan I think adding your own save() method on your List makes sense; then you could call save() on the individual objects sequentially, or whatever makes sense for your server.
That way, I don’t think you’d have to serialize them first and then figure out updating the “live” objects later…
RyanMilligan
@RyanMilligan
Jun 04 2018 21:46
I can't call them sequentially though, I want to run it through the existing behavior pipeline but end up just calling the server once, with an array containing all the instances that are being saved. The current pipeline seems not to support that, since it will e.g. try to find an id property on the list in order to put it in an instance store, which it will later fail to find it in.
Chasen Le Hara
@chasenlehara
Jun 04 2018 21:47
Ohh, so your server does support updating multiple objects in a single request?
RyanMilligan
@RyanMilligan
Jun 04 2018 21:56
Yes, or at least it can very easily. I actually currently receive a single object and then wrap it in a fake array for processing, so it's a very small amount of work to just have it use the array it was already passed.
Justin Meyer
@justinbmeyer
Jun 04 2018 21:57
I've just manually added a save function
to the list prototype
RyanMilligan
@RyanMilligan
Jun 04 2018 21:58
And it goes through the existing behavior pipeline like a normal save? Like, real-time to make sure everything gets sorted appropriately, the instances get hydrated so the ids that come back from the server get put in the appropriate places, etc?
with the data as it comes back
DefList.extend({
  ..
  save(){
    MAKE_REQUEST().then(function(updatedData){
      updatedData.forEach( (record)=> connection.updateInstance( record ) )
    })
  }
})
RyanMilligan
@RyanMilligan
Jun 04 2018 22:00
Ok, gotcha. So you're saying, craft my own request to submit the save, then call into the pipeline with the data that comes back?
Justin Meyer
@justinbmeyer
Jun 04 2018 22:00
yeah
well, the one thing that wont be setup ... is isSaving()
you could do that yourself like:
DefList.extend({
  ..
  save(){
   this.forEach(function(instance){
    canReflect.setKeyValue(instance, "_saving", true);
   })

    MAKE_REQUEST().then(function(updatedData){
      updatedData.forEach( (record)=> connection.updateInstance( record ).then(function(instance){ 
         canReflect.setKeyValue(instance, "_saving", false); } )
    })
  }
})
please write this up in the forums once you get it working :-)
(or create a behavior for it :P)