-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add ability to easily filter the results of findDescendantModels #2058
Comments
OK I'm obviously being a bit thick here and completely failing to realise that you could just do: _.where(Adapt.findById('co-05').findDescendantModels('components'), {'_isAvailable': true, '_isOptional': false}); I blame the code in PLP's completionCalculations.js which uses However, still might be useful to include this 2nd parameter in the function anyway just to make it even easier? i.e. Adapt.findById('co-05').findDescendantModels('components', {'_isAvailable': true, '_isOptional': false}); |
You can't do the |
ah of course it does! phew... |
You might find http://underscorejs.org/#isMatch useful for comparing the if (options && options.filterBy) {
return _.filter(models, function(model) {
return _.isMatch(model.toJSON(), options.filterBy);
});
} |
Oh that's interesting, I'll give that a try - definitely shorter than what I have at the moment: return _.filter(returnedDescendants, function(descendant){
for (var property in filterBy) {
var value = filterBy[property];
if (descendant.get(property) !== value) {
return false;
}
}
return true;
}); |
It might also be slower than what you have. |
Oh, I keep meaning to say, if you make the second parameter an object with sub attributes, it leaves space for future behavioural extensions (like sortBy). Adapt.findById('co-10').findDescendantModels('component', { filterBy: { _component: 'hotgraphic' } });
Adapt.findById('co-10').findDescendantModels('component', {
filterBy: {
_component: 'hotgraphic'
}
}); It can also make it read more clearly despite being a bit odd syntactically. |
It does seem to be... not the best test in the world but filtering the 30 components in OOTB Adapt using the I'll try and find some time to test against a larger amount of content tomorrow. |
so, in a course with 75 components, using |
With
adaptModel.findDescendants
, if you wanted to do something like get all available, mandatory components for a contentObject, you could do this:However, following #1616 this function is deprecated in favour of
findDescendantModels
- but as this function returns an Array rather than a Backbone Collection it's necessary to do the following to achieve the same result:Which is obviously quite a bit wordier compared to the original.
I think it would therefore be useful to amend
findDescendantModels
to have an optional 2nd parameterfilterBy
that would give us similar functionality to the original version.The text was updated successfully, but these errors were encountered: