Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

backbone.js: Button in view that affects a different model in collection

I'm just getting going with backbone.js. So far, I'm really liking it.

I have something like this:

  • ModelA
  • ModelB
  • ViewA
  • ViewB

ModelA holds a collection of ModelB

How can I build a ViewB of ModelB with a button which, when clicked, changes an attribute on the next ModelB instance in the collection?

like image 270
srmark Avatar asked Dec 02 '25 03:12

srmark


1 Answers

var col = this.model.collection;
var nextModel = col.at( col.indexOf(this.model) + 1)
if(nextModel) nextModel.set({whatevar});

You do not need to keep track of the parent collection, backbone does it for you. You should check if you are at the end of the collection also.

like image 191
Julien Avatar answered Dec 03 '25 19:12

Julien