Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember Data: how to set isDirty for a record?

I have a details: DS.attr('recursive_object') custom transformation (it's a recursive object).

The details attr can be edited in place, without the actual object reference changing (e.g., a sub-detail on the details attr is edited). This, however, doesn't trigger the parent record's isDirty flag.

How can I manually notify a DS.Model record that it's been updated?

Model definition:

App.MyRecord = DS.Model.extend
  details: DS.attr "recursive object"

Editing details

# record is an instance of App.MyRecord
# makes a change to the record's details attr 
record.makeChangeToDetails()
record.get('isDirty') # still false because Ember doesn't know that a sub-detail changed.

Things I've tried:

Wrapping record.makeChangeToDetails() with will/didSetProperty calls:

record.send 'willSetProperty', name: 'details'
record.makeChangeToDetails()
record.send 'didSetProperty', name: 'details'

Calling notifyPropertyChange

record.notifyPropertyChange 'details'

Calling set and passing it the same object

record.makeChangeToDetails()
record.set 'details', record.get('details')

I've also tried sending various other DS.model states events from here: https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/model/states.js including didChangeData, becameDirty but none of these worked.

Any ideas?

like image 596
Sherwin Yu Avatar asked Apr 22 '13 23:04

Sherwin Yu


2 Answers

I got some clues from this other question: How to manually set an object state to clean (saved) using ember-data

In particular, the bit about:

record.get('stateManager').transitionTo('loaded.saved')

like image 92
lobati Avatar answered Oct 25 '22 06:10

lobati


Using 1.0.0-beta.7+canary.b45e23ba, this appears to do the job:

> record.isDirty()
< false
> record.send('becomeDirty')
< undefined
> record.isDirty()
< true
like image 3
aceofspades Avatar answered Oct 25 '22 08:10

aceofspades