0

I have a backbone collection

var Stuff = Backbone.Collection.extend({
    url: "stuff/" 
    model: StuffModel
});

I also have an array of ids:

var ids = [ 1, 2, 3, 4 ];

As per the docs, I call fetch on Stuff like so:

this.collection.fetch( { $.param({ ids : exercise_ids.join( "," )})});

This sends a request to the server of the form:

/stuff/?ids=1,2,3,4

This works, but I'm not happy with the form of the request. Is there a way I can send the request with the following form (ie not use the querystring)

/stuff/1,2,3,4

Thanks (in advance) for your help.

user1031947
  • 6,294
  • 16
  • 55
  • 88
  • That code can't be working (see http://jsfiddle.net/EXwmp/1/). Have you copied it across wrongly? Could you post all the code for your collection? – net.uk.sweet Feb 23 '13 at 00:22

1 Answers1

0

Assuming the backend sees [param] as ids when you do /stuff/[param] then there is no difference in functionality. These request are made behind the scenes and don't affect the browser's address bar so there isn't really any concern here. If you want to format your url you can define url as a function in your Backbone Collection

var Stuff = Backbone.Collection.extend({

    initialize: function(models, options) {
        this.ids = options.ids

        //bind functions to 'this' so that you can access ids
        _.bind(this, 'setIds', 'url');
    },

    setIds: function(ids) {
        this.ids = ids;
        //return 'this' to allow chaining
        return this;
    },

    url: function() {
        return 'stuff/' + this.ids.join(',')
    }
});

myCollection.setIds([1,2,3,4]).fetch()
menzoic
  • 874
  • 1
  • 8
  • 10