0

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);
            },
        });
Ali
  • 5,338
  • 12
  • 55
  • 78
  • http://stackoverflow.com/questions/5757555/how-do-i-trigger-the-success-callback-on-a-model-save – Gohn67 May 19 '12 at 17:50
  • @Gohn67 Thanks for the comment. Mine is different because im calling the **model's** save function – Ali May 19 '12 at 17:53

1 Answers1

2

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.

mpm
  • 20,148
  • 7
  • 50
  • 55
  • I will update the code the way you suggested but I think I've tried before and did not work – Ali May 19 '12 at 17:53
  • Did not work. `urlToAdd.set({url: urlBody.value, tags:tagsToAdd}); urlToAdd.save(null, { success:function() { console.log('it works') } });` added an additional `}` because it was giving a syntax error – Ali May 19 '12 at 17:57
  • yep i forgot a } , create an error function , too in case your save action fails : – mpm May 19 '12 at 17:58
  • `urlToAdd.set({url: urlBody.value, tags:tagsToAdd}); urlToAdd.save(null, { success:function() { console.log('it works') } }, { error:function() { console.log('it does not work'); } });` does nothing. Sorry if I'm asking too much but it really prints nothing while my server side php saves the data to database – Ali May 19 '12 at 18:02
  • well , we need the rest of the model definition. Be sure to configure Backbone.sync too if your script is not a restfull api. – mpm May 19 '12 at 18:04
  • I've edited the question and I do not know how to configure the sync, kinda newbie so.. – Ali May 19 '12 at 18:08
  • I set up a small exemple here : http://jsfiddle.net/camus/rz8WT/ , and check your javascript console for any errors. – mpm May 19 '12 at 18:24
  • will it work if I applied to my example, in here you do not send any data to php thats why I'm asking – Ali May 19 '12 at 18:27
  • well the problem is first do you get errors in the console when you save your model , yes or, no, second , is your php script a restfull api or not , you did not answer that. – mpm May 19 '12 at 18:29
  • In my php I've basically check for the server's request and if it is a post I've save the url to my database, if that's what you mean. Sorry if I got it wrong – Ali May 19 '12 at 18:30
  • you did not answer me , do you get errors in the browser console or not ? – mpm May 19 '12 at 18:35
  • I do not and it succesfully saves the url to database – Ali May 19 '12 at 18:36
  • and what is the php script response ? – mpm May 19 '12 at 18:36
  • `PHP works I have received a .save() method from backbone ab.com {"Info":"Successfully placed"}` is my response. Using echo to print them – Ali May 19 '12 at 18:39
  • are you sure you have jQuery and underscore libraries before Backbone ? and did you empty your browser cache to reload the script ,etc ? because there is no reason why it would not work ? by the way , the success and error function must be in the same object, not in 2 separate objects . – mpm May 19 '12 at 18:44