Data: unloadRecord after v2.12

Created on 18 Jan 2018  路  10Comments  路  Source: emberjs/data

Hey :)

I have this simple piece of code (removing record from the store while notified by a websocket).

handleRemove: function(content) {
        var store = this.get("store");
        for (var model in content) {
            if (!this.get("ignoredModels").includes(model)) {
                var modelId = content[model]["id"];
                var object = store.peekRecord(store.convert(model), modelId);
                if (object) {
                    object.unloadRecord();
                }
            }
        }
    }

In ember-data 2.12 everything works as expected.
But after this version, I'm unable to track the origin (if someone know how to do it ?), but the store tries to re-fetch some unloaded models (and fail with a 404 from the server).
I'm also encoutering sometimes the Cannot read property 'eachAttribute' of null error that others seems to have on unload.

Most helpful comment

I've just tried upgrading to ED 3.0.1 and am still experiencing this issue.
See this simple test: https://gist.github.com/bastimeyer/ecb84112ba24082dfa58fdd3596ef978

All 10 comments

I worked around it with

   record.get('_internalModel').transitionTo('deleted.saved');

right after before unloading the model. All the null values vanished from the ManyArrays, RecordArrays, etc. Don鈥檛 forget to wrap in a runloop. Needs more tests, btw.

I've just tried upgrading to ED 3.0.1 and am still experiencing this issue.
See this simple test: https://gist.github.com/bastimeyer/ecb84112ba24082dfa58fdd3596ef978

unloadRecord should not be use for client-side delete. I would recommend building a solution overtop of deleteRecord instead.

@runspired I thought the recent work by @hjdivad around unloadRecord was to bring back this client-side delete. I'm just confused now.

@sly7-7 it brought it back for a small subset of cases where it could work.

In that case, I think I must rework my client-side delete process of the object in my app. But won't deleteRecord let some "zombies" records, and so my application will leak ?

If we shouldn't use unloadRecord how would you advise removing a record from the store?
The record I'm working with is loaded as part of a hasMany relationship.

Indeed. If a record gets deleted on server how you make ED understand that? The right way should be to issue a reload(), but the server will return a 404 and ED will just blow up. If I issue a destroyRecord() the server will return 404 too. deleteRecord will leave a zombie object in my collections. That is, you can鈥檛 differentiate between something that is new, was deleted or never existed.聽

TL;DR If server says me聽鈥渞ecord id 42 was deleted by some other user鈥澛爓hat was I supposed to do?聽

@JonathanBristow it's complicated :( I'll be looking into building an addon to experiment with this soon while we wait for json-api operations to finalize to give us a public api spec to follow.

A better discussion of the true heart of this issue is in https://github.com/emberjs/data/issues/5424

Closing in favor of that ticket.

Was this page helpful?
0 / 5 - 0 ratings