Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember-data - "Attempted to handle event `deleteRecord` on *record* while in state root.deleted.saved."

Tags:

ember.js

I'm trying to use ember-data within my app to manage follower/following relations. I'm having an issue where if the user hits the toggle on/off ember throws "Attempted to handle event deleteRecord on while in state root.deleted.saved." Any one run into this before?

Action code below

    actions: {
    follow: function(model){
        var component = this;
        var store = this.get('targetObject.store');
        var session = this.get('targetObject.session');
        this.set('isFollowed', true)

        /* Follower Side */
        Ember.RSVP.hash({
            profile: store.find('profile', model),
            follower: session.get('currentUser.profile')
        }).then(function(rsvp){

            var follower = store.createRecord('follower', {
                profile: rsvp.profile,
                follower: rsvp.follower,
                status: 'approved'
            });


            var followed = store.createRecord('followed', {
                profile: rsvp.follower,
                followed: rsvp.profile,
                status: 'approved'
            });

            followed.save().then(function(savedFollowed){
                rsvp.follower.get('followeds').addObject(savedFollowed);                    
            });
            follower.save().then(function(savedFollower){
                rsvp.profile.get('followers').addObject(savedFollower);
            });


        })
    },

    unfollow: function(model){
        var component = this;
        var store = this.get('targetObject.store');
        var session = this.get('targetObject.session');

        this.set('isFollowed', false)

        /* Remove Follower Side */
        component.set('isFollowed', false)
        Ember.RSVP.hash({
            profile: store.find('profile', model),
            follower: session.get('currentUser.profile')
        }).then(function(rsvp){

            /* Delete Follower Side */

            var follower_id = rsvp.follower.get('id');
            rsvp.profile.get('followers').forEach(function(item){
                if(follower_id == item.get('followLink')){                        
                    item.destroyRecord();
                }
            })

            var profile_id = rsvp.profile.get('id');

            rsvp.follower.get('followeds').forEach(function(item){
                if(profile_id == item.get('followLink')){                        
                    item.destroyRecord();
                }
            })
        })
    }  
}

UPDATE

I resolved the issue - thank you GJK for the response. For those who run into this issue - because I was adding the record to the parent models "hasMany" relation manually using "addObject" - when I deleted the record, I also needed to remove it from that relation so that it didn't exist in the parents "hasMany" and this come up in the delete loop again. Long story short the solution was to add 'removeObject(item)' i.e...

item.destroyRecord(); 
rsvp.follower.get('followeds').removeObject(item);

item.destroyRecord();
rsvp.profile.get('followeds').removeObject(item)                        
like image 978
Danny Avatar asked Jun 05 '14 22:06

Danny


1 Answers

root.deleted.saved means that your model has already been deleted, and the change has been persisted to the server. My guess would be that followers and followeds are sets of users that are not necessarily disjoint.

like image 78
GJK Avatar answered Nov 18 '22 10:11

GJK