I'm trying to create a menu in AngularJS using template and functional recursion. Unfortunately the code is using a lot of recursion so please lend some help.
Here is the menu data (in JSON):
var menu = {
'users': {title: 'Users', href: '/admin/users', icon: 'fa-user', 'priority': '2'},
'dashboard': {title: 'Dashboard', href: '/admin', icon: 'fa-home', badge: null, 'priority': '1'},
'expert': {title: 'Expert users', href: '', icon: 'fa-cog', 'priority': '3'},
'logins': {title: 'Social logins', href: '/admin/social-logins', icon: 'fa-lock', 'priority': '2', parent: 'expert'},
'config': {title: 'Site config', href: '/admin/config', icon: 'fa-cog', 'priority': '1', parent: 'expert'},
'security': {title: 'Site config', href: '/admin/config', icon: 'fa-key', 'priority': '1', parent: 'config'},
'deploy': {title: 'Deploy', href: '/admin/deploy', icon: 'fa-upload', badge: null, 'priority': '1'},
};
As you can notice some of the items have a property called parent that points to a parent key under which they will be nested.
Problem #1: Process this JSON and order it by priority. If an item has a parent key, create a children key (for template recursion) in the parent item and put them under it. I think this can be done easily with recursion but I just can't figure out how to do it.
So when the code is correct, it should give out the following output:
var final = [
{title: 'Dashboard', href: '/admin', icon: 'fa-home', badge: null, 'priority': '1', id: 'dashboard'},
{title: 'Users', href: '/admin/users', icon: 'fa-user', 'priority': '2', id: 'users'},
{
title: 'Expert users', href: '', icon: 'fa-cog', 'priority': '3', id: 'expert',
children: [
{ title: 'Site config', href: '/admin/config', icon: 'fa-cog', 'priority': '1', id: 'config',
children: [{title: 'Security settings', href: '/admin/security', icon: 'fa-key', 'priority': '1', id: 'security'}]
},
{title: 'Social logins', href: '/admin/social-logins', icon: 'fa-lock', 'priority': '2', id: 'logins'}
]
},
{title: 'Deploy', href: '/admin/deploy', icon: 'fa-upload', badge: null, id: 'deploy'}
]
Problem #2: Solved. Render this using AngularJS template recursion. I think I have figured this one using the examples. Here is my attempt to render it. The menu is rendering quite perfectly when the data is in the correct format.
Just need help with Problem #1. Create the final var from menu var (using recursion I guess).