Skip to content
This repository has been archived by the owner on May 11, 2018. It is now read-only.

destroyResource being called 2 times #11

Closed
thalisvilela opened this issue Mar 30, 2012 · 4 comments
Closed

destroyResource being called 2 times #11

thalisvilela opened this issue Mar 30, 2012 · 4 comments

Comments

@thalisvilela
Copy link

when i call .destroyResource() in an Em.Resource object, it fires the function 2 times, so generates 2 DELETE xhr requests.

I checked the button that fires the function that calls destroyResource, and its ok, calling it just one time, so i think it might be a issue with ember-rest.js. Any clue?

the code:


App.user = Em.Resource.extend({
    resourceUrl:"testObjects.php",
    resourceIdField:"_id",
    resourceProperties:['nome','email','id','senha']
});

App.users = Em.ResourceController.create({
    resourceUrl:"testObjects.php",
    resourceType:App.user
});

App.novoUsuarioView = Em.View.extend({
    criar:function(){
        var novoUsuario = App.user.create({nome:this.get("nome"), email:this.get("email"), senha:this.get("senha"), status:0});
        App.users.load(novoUsuario);
        novoUsuario.saveResource();
        App.log(novoUsuario.getProperties("nome","email", "senha"));
    },
    load:function(){
        App.users.findAll();
    }
})

App.usersView = Em.View.extend({
    contentBinding:'App.users',
    didInsertElement:function(){
        var self = this.$();
        self.find(".showUser").hover(function(){
            $(this).stop(true).animate({
                backgroundColor:"#bbb"
            },200)
            $(this).find(".userOptions").stop(true,true).fadeIn(200);
        }, function(){
            $(this).stop(true).animate({
                backgroundColor:"#ddd"
            },100)
            $(this).find(".userOptions").stop(true,true).fadeOut(100);
        });
        self.find(".edit").click(function(){
            self.find(".editUser").show(0);
        });
        self.find(".cancel").click(function(){
            self.find(".editUser").fadeOut(200);
        })
    },
    templateName:"usuarios",
    removerUsuario:function(){
        console.log("chamou a action");
        var object = this.get("usuario");
        this.$("div:first-child").slideUp(100, function(){
            //App.users.destroyResource(object);
            object.destroyResource().fail(function(e){
                console.log("Error");
                console.log(e);
            }).done(function(e){
                App.users.removeObject(object);
                console.log("success!");
                console.log(e);
            });
        });
        
    },
    editarUsuario:function(){
        var usr = this.get("usuario");
        view.appendTo("#holder");
    }
})

@dgeb
Copy link
Member

dgeb commented Mar 30, 2012

This doesn't seem to be a general problem with ember-rest. But please make sure you're using the latest version.

You're calling object.destroyResource() from within this.$("div:first-child").slideUp(). Perhaps div:first-child is selecting more than one element? Try calling destroyResource() directly and set a breakpoint and step through it if you're still seeing the problem.

Also, to follow convention when extending ember classes, start the names with caps (e.g. App.User). Objects like App.users start with lower case.

@thalisvilela
Copy link
Author

Right on the spot! The slideUp function is the one thats is being called two times, i would not expect it being called 2 times as i'm using first-child, but its selecting 2 elements. And thanks for the advise in the conventions!

@dgeb
Copy link
Member

dgeb commented Mar 30, 2012

Great - glad to help!

@thalisvilela
Copy link
Author

Just took a look at the jquery docs, the :first-child filter can match more than one element... i think its a bit contraditorious, but anyway, solved.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants