SkyeBoniwell
SkyeBoniwell

Reputation: 7112

method to interate over specific objects in a model

I have a Backbone parent model/view that contains a bunch of info like:

this.id;
this.title;
this.department;
this.country;
this.zipcode;

But it also contains different people like this:

this.president {memberName: "Joe"};
this.vicePresident {memberName: "Jill"};
this.underwriter {memberName: "Bob"};
this.sponser {memberName: "Yoda"};
this.coSponser {memberName: "Susan"};
this.owner {memberName: "Alice"};
this.informationManger {memberName: "Pascal"};

The parent view needs to instantiate a child view that displays each member in it's own text field like:

 <input type="text" class="{{ memberType }}" value="{{ memberName }}">

So it would like like:

 <input type="text" class="president" value="Joe" />
 <input type="text" class="vicePresident" value="Jill" />
 etc...

I thought about using "jQuery.each()" to move through the model, but that would put everything into an element.

I ended up with something like:

this.model.each(this.renderEachMember, this);

But like I said, that would interate over each object in the model and doesn't discriminate.

I just need the people to go into their own input element.

So my question is, how can I interate over the model in the parent view, pick out only the people, and then write each person to it's own child view?

Thanks!

Upvotes: 0

Views: 25

Answers (1)

omdel
omdel

Reputation: 921

Assuming that this.president returns an object and this.id, etc. do not. What if you iterated through the model and checked for the presence of the key memberName?

Either that, or blacklist the keys you don't want and use: _.omit(object, *keys) (see http://underscorejs.org/#omit ). _ would thereby be replaced with your this.model, in that case.

Here's their _ example:

_.omit({name: 'moe', age: 50, userid: 'moe1'}, 'userid');
=> {name: 'moe', age: 50}
_.omit({name: 'moe', age: 50, userid: 'moe1'}, function(value, key, object) {
  return _.isNumber(value);
});
=> {name: 'moe', userid: 'moe1'}

Conversely, there is also a _.pick: http://underscorejs.org/#pick

Upvotes: 1

Related Questions