Updating / Saving / Persisting an instance

Now lets change some values and save changes to the database… There are two ways to do that:

  1. // way 1
  2. task.title = 'a very different title now'
  3. task.save().then(() => {})
  4. // way 2
  5. task.update({
  6. title: 'a very different title now'
  7. }).then(() => {})

It's also possible to define which attributes should be saved when calling save, by passing an array of column names. This is useful when you set attributes based on a previously defined object. E.g. if you get the values of an object via a form of a web app. Furthermore this is used internally for update. This is how it looks like:

  1. task.title = 'foooo'
  2. task.description = 'baaaaaar'
  3. task.save({fields: ['title']}).then(() => {
  4. // title will now be 'foooo' but description is the very same as before
  5. })
  6. // The equivalent call using update looks like this:
  7. task.update({ title: 'foooo', description: 'baaaaaar'}, {fields: ['title']}).then(() => {
  8. // title will now be 'foooo' but description is the very same as before
  9. })

When you call save without changing any attribute, this method will execute nothing;