H. Adolf H. Adolf - 1 month ago 23
jQuery Question

Pass argument to backbone collection

Hi everyone I am studing backbone.js and I have a little question I want to make app where I pass arguments(id) to backbone collection and pass that argument to URL.
My code look like this.

app.mainCollection = Backbone.collection.extend({
model: MainModel,
url: 'localhost:8080/manageDb/main/getData'
});


Thanks in advance.

Answer

You can do it very easy. just use initialize function.

app.mainCollection  = Backbone.collection.extend({
   model: MainModel,
   initialize: function(options){
       this.id = options.id;
   },

   url: function(){
       return 'localhost:8080/manageDb/main/getData'+'/'+this.id;
   } 
});

var collection = new app.mainCollection ({id: 'anyId'});