{"action":"created","issue":{"url":"https://api.github.com/repos/adgsm/multi-level-push-menu/issues/140","repository_url":"https://api.github.com/repos/adgsm/multi-level-push-menu","labels_url":"https://api.github.com/repos/adgsm/multi-level-push-menu/issues/140/labels{/name}","comments_url":"https://api.github.com/repos/adgsm/multi-level-push-menu/issues/140/comments","events_url":"https://api.github.com/repos/adgsm/multi-level-push-menu/issues/140/events","html_url":"https://github.com/adgsm/multi-level-push-menu/issues/140","id":131556298,"number":140,"title":"Adding items on the same menu level in descending order (Possible bug)","user":{"login":"travislbrundage","id":8084860,"avatar_url":"https://avatars.githubusercontent.com/u/8084860?v=3","gravatar_id":"","url":"https://api.github.com/users/travislbrundage","html_url":"https://github.com/travislbrundage","followers_url":"https://api.github.com/users/travislbrundage/followers","following_url":"https://api.github.com/users/travislbrundage/following{/other_user}","gists_url":"https://api.github.com/users/travislbrundage/gists{/gist_id}","starred_url":"https://api.github.com/users/travislbrundage/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/travislbrundage/subscriptions","organizations_url":"https://api.github.com/users/travislbrundage/orgs","repos_url":"https://api.github.com/users/travislbrundage/repos","events_url":"https://api.github.com/users/travislbrundage/events{/privacy}","received_events_url":"https://api.github.com/users/travislbrundage/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-02-05T05:01:32Z","updated_at":"2016-02-05T09:57:54Z","closed_at":null,"body":"Hi there,\r\n\r\nSo I am attempting to use the `additems` function in order to add new items to my menu. I want to add them in descending order. So, when clicking the [ add item button ], it adds a new item to the bottom of the list. I thought using the position field would allow me to do this, but it doesn't work as I expect.\r\n\r\nHere is basically what I am doing:\r\n```\r\ntitle = \"Main menu\" // This is in the <h2> at the upper level of the menu\r\nindex = 0;\r\naddItem = function() {\r\nvar $addTo = $('#menu').multilevelpushmenu('findmenusbytitle', title).first();\r\nvar item = [ { // My data here } ];\r\n$('#menu').multilevelpushmenu('additems', item, $addTo, index);\r\nindex += 1;\r\n}\r\n```\r\nI have affirmed that the menu grabbed by $addTo is the correct menu, and index is what I expect it to be. It correctly adds the first item, but afterwards it will embed all future items within the last item added. To make it more clear...\r\n\r\nThis is what is happening:\r\n```\r\nMain Menu\r\n---------------\r\n[ add item button ]\r\n```\r\nOne click:\r\n```\r\nMain Menu\r\n---------------\r\nItem 1 >\r\n[ add item button ]\r\n```\r\nTwo clicks:\r\n```\r\nMain Menu\r\n---------------\r\nItem 1 >\r\n[ add item button ]\r\n```\r\nWhere is item 2? If I open Item 1...\r\n```\r\nItem 1\r\n--------\r\nItem 2 >\r\n[ item data ]\r\n```\r\nThe same thing will happen if I go back and add a third item. I will have to click into item 1, then into item 2 to see my item 3 embedded within it.\r\n\r\nThis is what I want to happen:\r\n```\r\nMain Menu\r\n---------------\r\n[ add item button]\r\n```\r\nOne click:\r\n```\r\nMain Menu\r\n---------------\r\nItem 1 >\r\n[ add item button ]\r\n```\r\nTwo clicks:\r\n```\r\nMain Menu\r\n---------------\r\nItem 1 >\r\nItem 2 >\r\n[ add item button ]\r\n```\r\nAnd so on.\r\n\r\nThe closest I can get is if I just set position to be 0. Ergo, `$('#menu').multilevelpushmenu('additems', item, $addTo, 0);` instead. But this has the following result:\r\n```\r\nMain Menu\r\n---------------\r\n[ add item button ]\r\n```\r\nOne click:\r\n```\r\nMain Menu\r\n---------------\r\nItem 1 >\r\n[ add item button ]\r\n```\r\nTwo clicks:\r\n```\r\nMain Menu\r\n---------------\r\nItem 2 >\r\nItem 1 >\r\n[ add item button ]\r\n```\r\nAs you can see, the order of the items is opposite of what I want. I want it descending, not ascending.\r\n\r\nFinally, notably, I can also set the position to 1 and it will work in a new way. `$('#menu').multilevelpushmenu('additems', item, $addTo, 1);` results in this:\r\n```\r\nMain Menu\r\n---------------\r\n[ add item button ]\r\n```\r\nOne click:\r\n```\r\nMain Menu\r\n---------------\r\n[ add item button ]\r\nItem 1 >\r\n```\r\nIn this situation, `position = 1` placed it below my [ add item button ]. This makes sense, but earlier when I was iterating through, at index = 0, it added the item in the main menu as expected, but then at index = 1, it placed the next item within the menu of the first item.\r\n\r\nIs this a bug with the `addItems` function? I'm still trying to step through the code, but looking at the javascript code in the library, it looks like it would function how I expect it to when iterating through. It just isn't for some reason. "},"comment":{"url":"https://api.github.com/repos/adgsm/multi-level-push-menu/issues/comments/180278692","html_url":"https://github.com/adgsm/multi-level-push-menu/issues/140#issuecomment-180278692","issue_url":"https://api.github.com/repos/adgsm/multi-level-push-menu/issues/140","id":180278692,"user":{"login":"adgsm","id":5786211,"avatar_url":"https://avatars.githubusercontent.com/u/5786211?v=3","gravatar_id":"","url":"https://api.github.com/users/adgsm","html_url":"https://github.com/adgsm","followers_url":"https://api.github.com/users/adgsm/followers","following_url":"https://api.github.com/users/adgsm/following{/other_user}","gists_url":"https://api.github.com/users/adgsm/gists{/gist_id}","starred_url":"https://api.github.com/users/adgsm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adgsm/subscriptions","organizations_url":"https://api.github.com/users/adgsm/orgs","repos_url":"https://api.github.com/users/adgsm/repos","events_url":"https://api.github.com/users/adgsm/events{/privacy}","received_events_url":"https://api.github.com/users/adgsm/received_events","type":"User","site_admin":false},"created_at":"2016-02-05T09:57:54Z","updated_at":"2016-02-05T09:57:54Z","body":"If you have mistyped\r\n`$('#menu').multilevelpushmenu('additems', item, $addTo, 0); `\r\ninstead of\r\n`$('#menu').multilevelpushmenu('additems', item, $addTo, index);`\r\nthan this is a bug, possibly on line\r\nhttps://github.com/adgsm/multi-level-push-menu/blob/master/jquery.multilevelpushmenu.js#L345\r\nwhere it should be\r\n`( position < ( $itemGroup.children( 'li' ).length ) && position >= 0 ) ?  `\r\ninstead of\r\n`( position < ( $itemGroup.find( 'li' ).length ) && position >= 0 ) ?  `\r\n\r\nSimple workaround for you could be to set index to -1,as follows\r\n`$('#menu').multilevelpushmenu('additems', item, $addTo , -1 );`\r\nwhich will then always trigger $.appendTo and as so add to the end of the queue.\r\n\r\nThanks for spotting this..."}}