我有一个使用Django / Tastypie后端和Backbone / Marionette前端的Web应用程序。我想使用Tastypie的bulk operations通过我的API使用PATCH请求创建多个对象到列表端点。
我的理解是Backbone不支持这一点。将此添加到Backbone的最佳方法是什么?我假设我需要向Backbone的集合对象添加一个save方法并扩展Backbone同步方法。
答案 0 :(得分:4)
If instead, you'd only like the changed attributes to be sent to the server, call
model.save(attrs, {patch: true}). You'll get an HTTP PATCH request to the server
with just the passed-in attributes.
Fiddle在骨干收集同步上发送补丁请求:
$(function() {
Backbone.$ = $;
var User = Backbone.Model.extend({
urlRoot: "/testUrl",
isNew : function () { return false; },
defaults: {
name: 'John Doe',
age: 25
}
});
var user1 = new User();
var user2 = new User();
var user3 = new User();
var user4 = new User();
var UserCollection = Backbone.Collection.extend({
model: User,
url: "/testUrl"
});
var userCollection = new UserCollection([ user1, user2, user3]);
// update
user1.set('name','Jane Doe');
user4.set('name','Another User');
// delete
userCollection.remove(user2);
// add
userCollection.add(user4);
userCollection.sync('patch', userCollection , { error: function () {
console.log(userCollection); } });
});