These are chat archives for canjs/canjs

17th
Feb 2016
Gregg Roemhildt
@roemhildtg
Feb 17 2016 20:55
Hi all. What's the best way to update a template bound to a viewModel that has a can.Model? I'd like the template to update after a model is created/saved/removed?
Kevin Phillips
@phillipskevin
Feb 17 2016 20:57
usually, I do something like this: http://jsbin.com/xemexoqeqi/1/edit?html,js,output
one property on the viewmodel for the promise returned by the model
and another for the data the promise returns
obviously it gets more complex than that when you have multiple operations happening on your Model
Gregg Roemhildt
@roemhildtg
Feb 17 2016 21:00
Okay, I'm doing something similar, but I'm doing the findAll. Do I have to call a method to update the template after calling .save?
Kevin Phillips
@phillipskevin
Feb 17 2016 21:01
I think it should update automatically
Gregg Roemhildt
@roemhildtg
Feb 17 2016 21:02
The template updates after calling object.destroy() but not after object.save().
Watching the network logs, the object is indeed updated, but there isn't a call to the findAll url after update.
Kevin Phillips
@phillipskevin
Feb 17 2016 21:03
right, it won’t do the GET again
you should just get the updated data back in the response to the save() call
is your save() creating a new item or updating an item?
Gregg Roemhildt
@roemhildtg
Feb 17 2016 21:07
I have a save doing both.
In one case, a new item is created, edited, and then save() is called on that new item.
In another case, an existing item is edited, then save() is called on it.
Kevin Phillips
@phillipskevin
Feb 17 2016 21:12
is save() returning back the list ?
Gregg Roemhildt
@roemhildtg
Feb 17 2016 21:14
No, it looks like the deferred returned resolves to the object that was edited.
Kevin Phillips
@phillipskevin
Feb 17 2016 21:19
ok, that makes sense
you can listen to events on the Model and use them to update your viewModel
I’m not sure if that’s the best way to do it though
someone else might have a better idea
Gregg Roemhildt
@roemhildtg
Feb 17 2016 21:23
If it helps, I made a simplified gist: https://gist.github.com/roemhildtg/f597e1987f78e48e79f3 there's a form part of it that calls the formSubmit.
Gregg Roemhildt
@roemhildtg
Feb 17 2016 21:30
I noticed the example you provided, you used value in the gamePromise, I'm using get does that make a difference?
value doesn't work since for me, since the model is getting passed in by the component.
Kevin Phillips
@phillipskevin
Feb 17 2016 21:33
value is just a default value
get sets up a binding
so if you used get it should return a new promise if your filter or group or objectModel changes
Gregg Roemhildt
@roemhildtg
Feb 17 2016 21:36
That makes sense.
Kevin Phillips
@phillipskevin
Feb 17 2016 21:39
this is another way you could do it: http://jsbin.com/wiwoyecene/1/edit?html,js,output
but you’ll have to handle it in each place you’re calling save
so it’s not a perfect solution
I’m sure someone has a better idea
Gregg Roemhildt
@roemhildtg
Feb 17 2016 21:44
Okay, I'll keep that approach in mind, I'll have to replace the existing object in the list when its edited probably too I think. Thanks for the assistance
Kevin Phillips
@phillipskevin
Feb 17 2016 21:47
You could also take a look at can-connect
I haven’t used it extensively, but I think it might handle a lot of the data synchronization for you
Gregg Roemhildt
@roemhildtg
Feb 17 2016 22:03
Do you know will can-connect/super-map eventually replace can.Model?
Kevin Phillips
@phillipskevin
Feb 17 2016 22:05
I haven’t heard that
would be a good question for the forums though
there’s been some discusson on the 3.x roadmap: http://forums.donejs.com/t/canjs-roadmap-discussion/75
Gregg Roemhildt
@roemhildtg
Feb 17 2016 22:49
I think I might have been going at this with the wrong approach, but I'd love a second opinion. Initially I thought it would be good to pass an extended can.Model into my component, and trigger all the query/edit/etc through the table component, but it may be better and more generic to do the query within the parent app and pass the can.Model.List into the table component.