0

How can i pass the model's attribute to collectionOptions? When i try to pass param0 like that, then this is referenced to window instead to MyModel instance:

var MyModel = Backbone.RelationalModel.extend({
    defaults: {
        param0: null,
    },
    relations: [
        {
            type: Backbone.HasMany,
            key: 'others',
            relatedModel: 'OtherModel',
            collectionType: 'OtherModelCollection',
            collectionOptions: {param0: this.get('param0')}
        }
    ]
});
mumu2
  • 661
  • 1
  • 5
  • 15

1 Answers1

1

From the fine manual:

collectionOptions

Value: an options hash or a function that accepts an instance of a Backbone.Relational Model and returns an option hash.

So just use a function instead of an object literal:

collectionOptions: function(m) {
    return {
        param0: m.get('param0')
    };
}

This "object or function" pattern is fairly common in the Backbone world, for example, Model#defaults behaves the same way.

mu is too short
  • 426,620
  • 70
  • 833
  • 800