0

I have a collection and I'm getting the models based on a property like this

var coll=reportWidgets.where({"is_editing":true});

I want to loop through these models and set the property is_editing to false.

How can I do this?

2 Answers 2

2
_.each(reportWidgets.where({is_editing: true}), function (reportWidget) {
  reportWidget.set('is_editing', false);
});

An alternative using _.invoke similar to @nikoshr

_.invoke(reportWidgets.where({is_editing: true}), 'set', 'is_editing', false);
Sign up to request clarification or add additional context in comments.

Comments

2

A different take on the problem, using _.chain and _.invoke for fun and, I hope, readability

reportWidgets.chain().filter(function(m) {
    return m.get('is_editing');
}).invoke('set', 'is_editing', false);

And a demo http://jsfiddle.net/nikoshr/PMvLC/

Note that in that context using where would produce unexpected results since it would work on an array of models and not on a collection and its proxied underscore methods.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.