Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

Correct replacing of list attribute while Model data update

$
0
0

The fiddle shows the problem

On init we have ```users``` list containing 3 items. We use standard findOne to get new data from server that contains new list, and we want old list to be replaced by new one. 

By default old list is merged with new one. If not use ```removeAttr: false``` option for Model, lists will be merged definitely incorrectly. So the only option is use ```removeAttr: true```, but not sure it if its a safe option, as may lead to unexpected removal of some attributes in nested maps.

What is the option without using ```removeAttr: true```?


Fiddle code:

  1. var Todo = can.Model.extend({
  2.     removeAttr: false //dont' want to use this for whole model
  3. },{   
  4.     
  5. });

  6. //to place model in store set _reqs
  7. can.Model._reqs = 1;

  8. var todo = new Todo({
  9.     id: 1,
  10.     title: 'title',
  11.     users: [{name: 'John', last: 'Galt'}, {name: 'Henk', last: 'Rearden'}, {name: 'Alex'}]
  12. })

  13. console.log('todo before update:', todo.users.length)

  14. // let say then I've got new users data from server using findOne
  15. // I don't want to mess with fixtures for this example, so Todo.model function will be called at the end of findOne 

  16. var newData = {id:1, users: [{name: 'Dagny'}, {name: 'Jane'}]}
  17. Todo.model(newData)


  18. // this function will merge old users with new users and it will be mess user will have 3 items, but should have olny 2.

  19. console.log('todo after update:', todo.users.length, todo.users.serialize())




Viewing all articles
Browse latest Browse all 3491

Trending Articles