roverred
roverred

Reputation: 1921

backbone.js: Prevent listener event from firing when model changes

I have the following listener in the view for when a model is changed:

this.listenTo(this.model, 'change', this.render);

When I change the model :

model.set('foo', bar);

Is it possible to make it not trigger the listener event for this specific function call? I still want the event to trigger at other calls.

Upvotes: 2

Views: 4044

Answers (1)

mu is too short
mu is too short

Reputation: 434695

From the fine manual:

Generally speaking, when calling a function that emits an event (model.set, collection.add, and so on...), if you'd like to prevent the event from being triggered, you may pass {silent: true} as an option. Note that this is rarely, perhaps even never, a good idea. Passing through a specific flag in the options for your event callback to look at, and choose to ignore, will usually work out better.

So if you don't want that specific set call to trigger a change event then:

model.set('foo', bar, { silent: true });

Or tunnel some information to render using a custom option:

model.set('foot', bar, { ignore_this: true });

and adjust render:

render: function(options) {
    if(options && options.ignore_this)
        return;
    // ...
}

Upvotes: 11

Related Questions