mobx-rest icon indicating copy to clipboard operation
mobx-rest copied to clipboard

calling save on a model should add itself to its collection

Open WesleyKapow opened this issue 6 years ago • 5 comments

Model.save on a new model should add itself to its collection (if it has one). This was the behavior I expected, especially after playing with destroy and how it correctly removes itself from its collection.

Simple example:

let newModel = collection.build();
await newModel.save();
collection.get(newModel.id) == newModel; // This is what is expected!

WesleyKapow avatar Feb 18 '19 17:02 WesleyKapow

You could use collection.create

collection.create({ name: 'Jon', last_name: 'Doe' })

andresgutgon avatar Feb 18 '19 18:02 andresgutgon

True, but we often deal with models that are either new OR existing (new/editing). It'd be nice to just call model.save() and have it add itself to the collection, instead of needing to do:

model.isNew ? model.collection.create(model) : model.save();

WesleyKapow avatar Feb 18 '19 18:02 WesleyKapow

This makes a lot of sense!

masylum avatar Feb 19 '19 09:02 masylum

https://github.com/masylum/mobx-rest/pull/79

masylum avatar Aug 08 '19 10:08 masylum

this appears to be a dupe and should be closed.

jeacott1 avatar Jan 15 '24 02:01 jeacott1