How to manipulate record state manually?


#1

Hi all, I am working for a project which use websock and embed-data to fetch data from server.

In some cases, I fetch data changes via websock from server and update the record, like code followed:

whenDataChanges: (data) ->
    @store.find("comment", data.id).then((comment) ->
        comment.set('text', data.text)
    )

When i set property of comment record manually, it’s state become dirty, which make the problem. Because the record comes from the server, I don’t want to call .save() method to remove the state(my request payload is huge).

I searched STO and the forum, found some topics about it:

but them don’t work for my case. How can i remove the dirty state? Thank you all.


#2

I think you could try the push method of DS.Store. This way you your model won’t get dirty.

http://emberjs.com/api/data/classes/DS.Store.html#method_push

For example in your case

var controller = this;
...
// in the success part of your promise:
controller.store.push('comment', { id: data.id, text: data.text });

#3

Maybe you could manually set the state:

comment.set('currentState', DS.RootState.loaded.saved);

You can have a look at the DS.RootState object here http://emberjs.com/api/data/classes/DS.RootState.html.

Best Regards, toovy


#4

If you were doing this to set one property of many, would the exclusion of the other properties have any effect?


#5

If I exclusion the others properties, they will became empty or zero.


#6

push has a third parameter: _partial

I guess if you pass true, the other properties won’t be affected.

controller.store.push('comment', { id: data.id, text: data.text }, true);

#7

Wow, the parameter does the work. Thank you @splattne!

Also thank @toovy, Your code works too, but I prefer to use push method.