Similar to ActiveRecord, you can simply call #save() on a model
instance. Spraypaint will create (POST) or update (PATCH) as needed.
#save() returns a Promise that will resolve a boolean - true
when the server returns a 200-ish response code, false when the server
returns a 422 response code (see
validations). As always, anything else will
reject the promise.
After saving, the instance will automatically pick up any
If a Model was instantiated with data from the server, isPersisted
will return true. This means that we can assign IDs on the client
without any adverse behavior; we can also manually mark objects as
persisted for testing purposes:
Notably, only dirty (changed) attributes will be sent to the server. This prevents race conditions and unexpected side-effects. In the following example, Post has attributes title, description, and createdAt: