dsm42026 2012-05-19 17:43
浏览 39

Backbone.js:Save的成功函数根本不起作用

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);
            },
        });
  • 写回答

1条回答 默认 最新

  • douzou0073 2012-05-19 17:52
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗