[英]Override sync method in backbone
我正在做一個名為person的模型,我使用parse.com javascript API。 將模型發送到parse.comì已經創建了我的函數send,但是我認為這是錯誤的。 我認為我必須使用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;
});
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,所以也許這不是解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.