php - Backbone.js: Save's success function simply does not work

205

I'm trying to get the most basic thing running in Backbone.js but somehow fail. The code I've produced for saving a url model into the server.

urlToAdd.set({url: urlBody.value, tags:tagsToAdd});
                urlToAdd.save({
                    success:function(){
                        console.log('it works');
                    }
                });

It successfully sends the data to my php page however I can not get successevent to work. Where am I doing wrong? I've carefully observed and couldn't find anything.

Here is my model definition: (Unfortunately since I am backbone noob I've applied this by checking a tutorial and I have no idea if the codes in url function are necessary

var URLWithTags=Backbone.Model.extend({
            initialize:function(){
                console.log('URL object has been initialized');
            },
            defaults:{
                url:'not defined',
                tags:[]
            },
            validate:function(attributes){
            //console.log(attributes.tags[0].length);
                /*if(attributes.tags[0].length<1)
                    return "You should create at least one tag";*/
            },
            urlRoot:'/URLTags/manage.php',
            url:function(){
                var base = this.urlRoot || (this.collection && this.collection.url) || "/";
                console.log("Base has been produced");
                if(this.isNew())
                    return base;

                return base+"?id="+encodeURIComponent(this.id);
            },
        });
271

Answer

Solution:

Save should be the second argument in an object when you call the save function. please people read the doc , or the source it is well commented ,short and easy to read.

urlToAdd.save(null,{success:function(){console.log('it works')}});

according to the doc :

savemodel.save([attributes], [options]) ;

so :

urlToAdd.save({},{success:successCallback , error:errorCallback}); 

EDIT

Exemple :

    var Car,yukon,$target ;
    Object.prototype.toString = function(){
        return JSON.stringify(this);
    };

    Car = Backbone.Model.extend({
        url:"/echo/json"
    });
    yukon= new Car({"color":"red","brand":"GM"});
    $target = $("#target");

    $target.append("describe yukon :",yukon.toString(),"<br/>");

    yukon.save({},
{
success:function(o){$target.append("save success <br/>",o.toString())},
error:function(e){$target.append("error",e)}
}
); 

where $target is a jquery object representing a div with the id of target.

People are also looking for solutions to the problem: php - Instagram API $data returning NULL

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.