Use invoke to change properties on each model in Backbone collection

October 03, 2014JavaScriptBackbone.js

Here's a quick tip. If you need to set the same property on each model in a Backbone collection check out the following snippet.

this.collectionName.invoke("set", {
  isSet: true,
});

Invoke can be used as a slightly cleaner version of this.

this.collectionName.each(function (model) {
  model.set({
    isSet: true,
  });
});

Happy coding!

The author, Jason Merino, with sun glasses and a hat

Jason Merino 💻 🚀

Software engineer, TypeScript enthusiast, avid gardener, all around family man, Franciscan at heart, celiac, aphantasiac. I enjoy nature and a good technical manual.

Follow me on Twitter and checkout my code on Github!