1

My trivial CRUD REST design looks like this:

create: [USERID]/weights/
read:   [USERID]/weights/[ITEMID]
update: [USERID]/weights/[ITEMID]
delete: [USERID]/weights/[ITEMID]

I tried backgrid and methodToURL. All I've been achieving is:

create: [USERID]/weights/
read:   [USERID]/weights/
update: [USERID]/weights/
delete: [USERID]/weights/

i.e. the ITEMID isn't passed at all. Even without methodToURL backgrid does not pass the ITEMID. Now I'm lost. Any suggestions?

Here's my impl. attempt:

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 } );
Ta Sas
  • 9,573
  • 15
  • 51
  • 73

1 Answers1

3

Looks like you are missing idAttribute. It appears the model does not detect its id. Are you using mongodb? If so then idAttribute should be _id, as in:

Backbone.Model.extend({
   idAttribute : "_id"
});

If not you then you should use something else that maps to your primary key (if it is not 'id').

I just handled a situation I think resembles this for when I want to use a list to bootstrap a Backbone.PageableCollection. The fix violates the Backbone.Collection anyways but it's the quick solution I could use. I don't know why if I pass #url in .extend(options) for Backbone.PageableCollection it doesn't get to the models but if I initialize it with a url the url gets passed to model instances as is hence all the models get #url{string} without the ids appended. After some searches/modifications I decided to just give the model definition #urlRoot.

Backbone.PageableCollection.extend({
   model : Backbone.Model.extend({
      urlRoot : '...'
   })
});