4

I have some global parameters that I want to be sent in every time I call a fetch on a collection... my issue is I don't want to declare the data: { ... } every time I fetch.

Is there a way I can provide default parameters inside the Collection itself with the possibility to add more or override some?

For example:

Instead of doing this every time:

this.articlesCollection.fetch({
    dataType: 'jsonp',
    data: {
        deviceType: GlobalVars.deviceType,
        memberId: GlobalVars.memberId,
        authToken: GlobalVars.authToken,
        targetObjectId: userId,
        limit: 50,
        excludeArticleBodies: true,
        excludeViewedItems: false
    },
    success: function() {
        _this.render();
    }
});

I'd like to just provide a one or two parameters and a success function, like this:

this.articlesCollection.fetch({
    data: {
        targetObjectId: userId
    },
    success: function() {
        _this.render();
    }
});

... and have the Collection look something like:

define([
  'underscore',
  'backbone',
  'global',
  'utilities',
  'models/article/ArticleModel'
], function(_, Backbone, GlobalVars, Utils, ArticleModel){

  var ArticlesCollection = Backbone.Collection.extend({

      model: ArticleModel,

      initialize : function(view) {
          this.view = view;
      },

      dataType: 'jsonp',
      data: {
          deviceType: GlobalVars.deviceType,
          memberId: GlobalVars.memberId,
          authToken: GlobalVars.authToken,
          limit: 50,
          excludeArticleBodies: true,
          excludeViewedItems: false
       },

      url : function() {
        return GlobalVars.baseAPIUrl + '/API/GetArticles';
      },

      parse : function(data) {  
        return data.Articles;
      }     

  });

  return ArticlesCollection;

});
Adam Storr
  • 1,438
  • 2
  • 21
  • 46

2 Answers2

3

Here's a working jsFiddle with one approach: http://jsfiddle.net/LEuGq/1/

Basically, you configure both an object of defaultParams and params as properties of your collection, which are used to dynamically compute the correct URL when fetch() is called. This way is probably more in alignment with backbone than changing the API of fetch() to accept parameters, which it is not designed to do.

var ParamCollection = Backbone.Collection.extend({

    defaultParams: {deviceType: 'raceCar', limit: 42},
    params: {},
    url: function() {
      return "/paramcollection?" + $.param(_.defaults(this.params, this.defaultParams));
    }
});
var paramCollection = new ParamCollection();
paramCollection.params.excludeArticleBodies = true;
paramCollection.params.limit = 52;
$("#debug").append(paramCollection.url());
Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • I know this is quite an old answer, but could you explain why it is that you use a separate object for `defaultParams` as opposed to setting some initial values within the `params` object which can then be overridden? – Ed_ Jun 17 '13 at 20:27
  • Just for clarity of intent, but I think it would also work with a single `params` property. – Peter Lyons Jun 17 '13 at 21:12
1

Backbone uses jQuery's ajax call by default, so you can set up anything you need as a default using various methods. See this question for some examples: jQuery's ajaxSetup - I would like to add default data for GET requests only

Community
  • 1
  • 1
Derick Bailey
  • 72,004
  • 22
  • 206
  • 219
  • I use this for some things, like cache, etc... but some of the variables I want to use are collection specific. – Adam Storr Dec 18 '12 at 23:36