Why does a newly created record has hasDirtyAttributes to true?

I’m creating a record using store.createRecord('record'); which is part of another record with a belongsTo relationship.

When I create this record, it gets created with hasDirtyAttributes to true already, making an action button appear whereas I only want to it to appear if the user start filling the form fields.

I was thinking of saving the record after its creation but I don’t have any POST call associated with this model, it’s part of another object that get saved later. I don’t know if that would have fixed the problem.

Is that normal? And if that’s the expected behavior, how can I set hasDirtyAttributes to false upon creation? If that’s not the expected behavior, anyone has an idea why this is happening?

Thanks!

From the docs

If this property is true the record is in the dirty state. The record has local changes that have not yet been saved by the adapter. This includes records that have been created (but not yet saved) or deleted

So I do think this is expected. The record also has a dirtyType flag which will be "created" in this case so you could use the dirtyType as additional information for managing your button state?

Thanks for the suggestion.

Unfortunately dirtyType appears to remain created until it is persisted also.

I finally found that thread on stack overflow about this issue, but it seems there is actually no way around my problem for now. There is no attributes that I can compute or observe that would be false upon creating my object, and becomes true if any of its properties change, regardless of persisting it via save().

I am not sure hasDirtyAttributes being true upon creation makes sens to me, especially with dirtyType already providing this information (was created or updated). hasDirtyAttributes should be true only when a propriety changes. Seems redundant now and not exactly useful. Right now there seem to be no way for a newly created object to detect changes via computed or observed properties if it’s not persisted.

Thanks again.

I think the source of truth for any ember data record is supposed to be the API, so if you think about it from that perspective it makes sense. Until something is persisted it’s “dirty”, and if it’s a new record the server has no idea of it at all yet so it’s “dirty” no matter what has changed on it.

I think your use case is why addons like ember model fragments (https://github.com/lytics/ember-data-model-fragments) were created. That might have some octane compatibility issues at the moment but it sounds like it fits your use case exactly now that you’ve described it more.

Thank you very much, ember model fragments looks like a promising solution, although reading the doc it seems that it does not support belongsTo relationship, and my child model is a belongsTo the parent model. I’ll still try to see if I can make it work.

Thanks again for your help!