0

我正在做一个名为 person 的模型,我使用 parse.com javascript api。将模型发送到 parse.comì 已经创建了一个我的函数发送,但我认为这是错误的。我认为我必须使用 api parse.com 覆盖同步方法,并在创建的模型上使用保存方法之后。这是正确的?

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;

});
4

1 回答 1

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,所以也许这不是解决方案。

于 2013-05-10T13:49:14.640 回答