derbyjs / racer

Realtime model synchronization engine for Node.js
1.19k stars 118 forks source link

Why does this update model locally? #160

Closed ile closed 11 years ago

ile commented 11 years ago

I modified the default app a little, added list.edit: https://github.com/ile/derbytest7/commit/c0e1b6755eed82122221fa6fdc9f831991ab6e28

What it does is just that it changes the .name property of the object, which is returned from e.get ":item".

What happens next is that the name property changes in the list of items after I click the menu item List on top menu bar on the browser window. If I do a hard refresh, the item name goes back to original.

Should this really work like this? I believe e.get ":item" is the same as model.at(el).get() which should be just a dumb object, right? Changing the properties of that object should not change any models?

Thanks...

nateps commented 11 years ago

You can't assign properties of a model object directly, since Racer won't know that anything has changed.

item.name = 'name changed...'

must be

item.set('name', 'name changed...')

On Sun, Sep 15, 2013 at 7:55 PM, Ilkka Huotari notifications@github.comwrote:

I modified the default app a little, added list.edit: ile/derbytest7@ c0e1b67https://github.com/ile/derbytest7/commit/c0e1b6755eed82122221fa6fdc9f831991ab6e28

What it does is just that it changes the .name property of the object, which is returned from e.get ':item'.

What happens next is that the name property changes in the _list of items_after I click the menu item List on top menu bar on the browser window. If I do a hard refresh, the item name goes back to original.

Should this really work like this? I believe e.get ":item" is the same as model.at(el).get() which should be just a dumb object, right? Changing the properties of that object should not change any models?

Thanks...

— Reply to this email directly or view it on GitHubhttps://github.com/codeparty/racer/issues/160 .

ile commented 11 years ago

I know, but it was exactly what I wanted to do: I wanted to change the dumb object, not change the model. But, to my surprise, the model changed (locally, not remotely).

There is a use case for this: I want to edit an object and update the model only after the user clicks "Save", and the easy way to do this is to

So... I really just wanted to change the dumb object, but it seemed to change the model locally, which was a problem.

psirenny commented 11 years ago

I ran into that problem as well, wrongly assuming that model.get() cloned the data.

nateps commented 11 years ago

Model.get does not clone anything, since that would add a ton of extra overhead and break tests of object equality.

If you want to modify properties of the object returned from the model but not actually change the object in the model, you have to clone it first. You can use require('derby').util.deepCopy if you want to do this.

On Mon, Sep 16, 2013 at 11:33 AM, Dennis notifications@github.com wrote:

I ran into that problem as well, wrongly assuming that model.get() cloned the data.

— Reply to this email directly or view it on GitHubhttps://github.com/codeparty/racer/issues/160#issuecomment-24533055 .

ile commented 11 years ago

OK, thank you. That's what I ended up doing, I just wanted to ask if this is an oversight or intended.

People might run into this again - so maybe informing on this somehow would be a good idea.

nateps commented 11 years ago

Yeah, this would be a good thing to add to the docs.