我琐碎的 CRUD REST 设计如下所示:
create: [USERID]/weights/
read: [USERID]/weights/[ITEMID]
update: [USERID]/weights/[ITEMID]
delete: [USERID]/weights/[ITEMID]
我尝试了 backgrid 和 methodToURL。我一直在实现的是:
create: [USERID]/weights/
read: [USERID]/weights/
update: [USERID]/weights/
delete: [USERID]/weights/
即ITEMID 根本没有通过。即使没有methodToURL backgrid 也不会传递ITEMID。现在我迷路了。有什么建议么?
这是我的暗示。试图:
var Weight = Backbone.Model.extend({
urlRoot: "weights",
initialize: function() {
Backbone.Model.prototype.initialize.apply(this, arguments);
this.on("change", function(model, options) {
console.log("Saving change");
if (options && options.save === false)
return;
model.save();
});
},
methodToURL: {
'read': '/' + sesUserId +'/weights/',
'create': '/' + sesUserId +'/weights/',
'update': '/' + sesUserId +'/weights/',
'delete': '/' + sesUserId +'/weights/'
},
sync: function(method, model, options) {
options = options || {};
options.url = model.methodToURL[method.toLowerCase()];
Backbone.sync(method, model, options);
}
});
var PageableWeightTable = Backbone.PageableCollection.extend({
model: Weight,
url: '/' + sesUserId +'/weights/',
state: {
pageSize: 10
},
mode: "client" // page entirely on the client side
});
var weightTable = new PageableWeightTable();
var grid = new Backgrid.Grid({
columns: [{
// name is a required parameter, but you don't really want one on a select all column
name: "",
// Backgrid.Extension.SelectRowCell lets you select individual rows
cell: "select-row",
// Backgrid.Extension.SelectAllHeaderCell lets you select all the row on a page
headerCell: "select-all"
}].concat(columns),
collection: weightTable
});
var $divWeightTable = $("#divweighttable");
$divWeightTable.append(grid.render().$el);
var paginator = new Backgrid.Extension.Paginator({
collection: weightTable
});
$divWeightTable.append(paginator.render().$el);
weightTable.fetch( { reset: true } );