Updating (i.e., saving) and destroying records are the next logical steps
after creating and retrieving. (There is a mnemonic for this: CRUD, which stands for “create, read, update, destroy.”) The methods
save and destroyRecord
are available directly on model instances. These methods
trigger requests through the adapter to update the data store.
They return Promise
objects, so you can chain callbacks with
.then
to do something with the returned data.
As you saw in this chapter, set is the
way to change property values on your model records. If you change
a value locally in the app, the data will be different from the
persisted source. Therefore, after you change a value with
set, you should save your data. You can do this with
modelRecord.save. Saving a model will tell
the store
to make a request to the API with a
POST
or PUT
, depending on the
state of the record.
Although this was not mentioned above, when retrieving data the
store
will make get requests
for data. When saving data, requests are sent with a type
POST
when the data does not exist in the
persisted source and PUT
when the data does
exist and is being updated.
When you call createRecord, as noted earlier, you are not saving the data to the database. You are merely making an in-memory object. Calling save is for creation and updating.
Destroying records is the last step. Like the methods for retrieving and updating records,
modelRecord.destroyRecord uses a request method –
in this case DELETE
– to remove a record from the persisted
source. The store
then removes the record
from memory. Like save,
destroyRecord is actually
two function calls in one, deleteRecord and
save, because deleteRecord only
deletes the record locally.
destroyRecord is more commonly used because it combines these steps into a single action.
18.191.176.5