Skip to content

Instantly share code, notes, and snippets.

@larsthorup
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larsthorup/20a8ffe68757c21c100f to your computer and use it in GitHub Desktop.
Save larsthorup/20a8ffe68757c21c100f to your computer and use it in GitHub Desktop.
Navigation sample code
define(['jquery', 'Q', 'evaluator'], function ($, Q, Evaluator) {
var Navigation = {};
var search = function (tree, propertyName) {
if ($.isArray(tree)) {
for (var i = 0; i < tree.length; ++i) {
return search(tree[i], propertyName);
}
} else if ($.isPlainObject(tree)) {
for (var pName in tree) {
if (tree.hasOwnProperty(pName)) {
var subTree = tree[pName];
if (pName == propertyName) {
result.push(subTree); // what to do here?
} else {
return search(subTree, propertyName);
}
}
}
}
};
Navigation.discover = function (options) { // wizletInfo
var propertyName = options.propertyName || 'navigation';
var navigationItems = [];
search(options.wizletInfo, propertyName, navigationItems);
return navigationItems;
};
return Navigation;
});
require(['Q', 'navigation'], function (Q, Navigation) {
module('Navigation');
test('discover', function () {
// setup
var wizletInfo = {
navigation: { title: 'top-level' },
content: {
navigation: { title: 'nested' },
rows: [
{
navigation: { title: 'in array' }
}
]
}
};
// invoke
var navigationItems = Navigation.discover({
wizletInfo: wizletInfo,
});
// verify
deepEqual(navigationItems, [{ title: 'top-level' }, { title: 'nested' }, { title: 'in array' }], 'navigationItems');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment