Skip to content

Commit

Permalink
issue/adaptlearning#2180 Return if no defined _children type (adaptle…
Browse files Browse the repository at this point in the history
…arning#2181)

* issue/adaptlearning#2180 Return if no defined _children type

* issue/adaptlearning#2180 Refactored findDescendantModels & updated to account for contentObjects

* issue/adaptlearning#2180 Removed unnecessary comparison
  • Loading branch information
oliverfoster authored and moloko committed Sep 3, 2018
1 parent 8715fee commit 5a58569
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions src/core/js/models/adaptModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,40 +260,26 @@ define([

/**
* Returns all the descendant models of a specific type
* @param {string} descendants Valid values are 'contentObject', 'article', 'block' or 'component'
* @param {string} descendants Valid values are 'contentObjects', 'pages', 'menus', 'articles', 'blocks' or 'components'
* @param {object} options an object that defines the search type and the properties/values to search on. Currently only the `where` search type (equivalent to `Backbone.Collection.where()`) is supported.
* @return {array}
* @example
* //find all available, non-optional components
* this.findDescendantModels('components', { where: { _isAvailable: true, _isOptional: false }});
*/
findDescendantModels: function(descendants, options) {
var returnedDescendants;
var allDescendants = [];
var flattenedDescendants;

function searchChildren(models) {
for (var i = 0, len = models.length; i < len; i++) {
var model = models[i];
allDescendants.push(model.getChildren().models);
flattenedDescendants = _.flatten(allDescendants);
}

returnedDescendants = flattenedDescendants;

if (models.length === 0 || models[0]._children === descendants) {
return;
} else {
allDescendants = [];
searchChildren(returnedDescendants);
}
var types = [
descendants.slice(0, -1)
];
if (descendants === 'contentObjects') {
types.push.apply(types, ['page', 'menu']);
}

if (this._children === descendants) {
returnedDescendants = this.getChildren().models;
} else {
searchChildren(this.getChildren().models);
}
var allDescendantsModels = this.getAllDescendantModels();
var returnedDescendants = allDescendantsModels.filter(function(model) {
return _.contains(types, model.get("_type"));
});

if (!options) {
return returnedDescendants;
Expand Down

0 comments on commit 5a58569

Please sign in to comment.