覆盖骨干网中的同步方法

时间:2013-05-10 13:42:37

标签: javascript backbone.js

我正在做一个名为person的模型,我使用parse.com javascript api。要将模型发送到parse.com“已经创建了我的函数发送,但我认为这是错误的。我认为我必须使用api parse.com覆盖sync方法,并在创建模型后使用save方法。这是对的吗?

var Person = Backbone.Model.extend({
        defaults: {},

        initialize:function() {
            console.log("inperson");
        },

        validate:function() {
            console.log("validate");
        },

        send:function() {
            var user = new Parse.User();
            user.set("username", this.get("username"));
            user.set("password", this.get("password"));
            user.set("email", this.get("email"));

            user.signUp(null, {
                success: function(user) {

                },
                error: function(user, error) {
                    alert("Error: " + error.code + " " + error.message);
                }
            });
        }
    });

return Person;

});

1 个答案:

答案 0 :(得分:0)

Backbone只使用一种同步方法(Backbone.sync)。与服务器“交谈”的所有方法集合和模型都经过这一处 您可以通过说:

简单地覆盖它
Backbone.sync = function(method, model, options) {
    // method is send through methodMap witch is an object:
    //var methodMap = {
    //    'create': 'POST',
    //    'update': 'PUT',
    //    'patch':  'PATCH',
    //    'delete': 'DELETE',
    //    'read':   'GET'
    //};

    // model refers to the active model and you can use model.attributes to get all the attributes. 

    // So in here you can write your integration with parse.com and not change anything else while using backbone.
    // Remember to trigger `sync` etc.        
};

但是我可以看到parse.com已经有了一个REST-api所以也许这不是解决方案。