Updating record excluding some attribute in ember data

Hi all,

In Edit display, I have one checkbox (model.enabled) and other text input(model.name,model.age) and save button.

On clicking save. I need to update all attribute(This work fine).

On changing checkbox, i want to only update model.enabled excluding model.name and model.age even though value for model.name and model.age is changed.

Currently, on updating model.enabled, ember data also update model.name and model.age if values are changed. I am using json api .

Thanks in advance.

Cheers, Santhosh

This is the default behavior of Ember Data. All attributes are persisted to the backend when a model is saved. I think one of the reasons for this is that a backend might typically consider a null/missing value to be cleared out vs just “not changed”. So it depends on your backend.

When you say “ember data also update model.name and model.age if values are changed” do you mean that those values have changed (become “dirty”) but you don’t want to send those updates to the backend for some reason? Ember Data is intended to send ANY updates you make to a record.

That said there are a couple things you could try depending on your use case.

  1. Typically if you don’t want those things updated if the model is “disabled” you may want to disable any form controls and/or rollbackAttributes before setting the enabled value. This simply prevents the user from updating those values so they don’t change.

  2. If what you really want is sending a “slim” payload to the server if enabled: true (e.g. { enabled: false } and stripping away all the other attributes) then you could do this in the serializer serialize method. But your backend would have to be expecting this.

  3. If you just want to clear out the values of name and age you could of course set them to null.

2 Likes

Thanks @dknutsen . It Helps :slight_smile:

Hi @dknutsen

I have another query.

I have model which contain configuration data for example conf {a,b,c,d}.

on updating conf, if value for b and c changes, i want to send another attribute for example “inconsistentConfiguration: true”.

How should i send this parameter “inconsistentConfiguration” in update request of conf model.

if i include “inconsistentConfiguration” in conf model, it only passed in first update request and not passed in second request(because “inconsistentConfiguration” is not become dirty).

I’m no sure what you mean by this part. Ember Data will send the entire model the first AND second time you send the request. Unless you’ve written serializer code to only send dirty changes. In which case you’re now fighting your own code.

Do you need this inconsistentConfiguration value in your templates? Or is it just something you want to send to your server and the front-end doesn’t care about it? If that’s the case I’d just handle this in the serializer serialize method.