jQuery-contextMenu icon indicating copy to clipboard operation
jQuery-contextMenu copied to clipboard

Menu item container should be a list

Open neonrust opened this issue 10 years ago • 7 comments

Currently the "items" container in the menu definition is of type Object.

This is risky (at best), as the property order is not guaranteed (see: http://stackoverflow.com/a/5525820/1111580)

Instead, a better type would be a list, e.g. storing the items like so:

selector: '.my-menu',
items: [
    { key: 'item1', name: 'Item 1' },
    {  ...  },
],

Of course, key I got from the top of my head... probably something more relevant could be chosen.

neonrust avatar Sep 30 '15 06:09 neonrust

From same stackoverflow topic:

If you rely on insertion order, you are outside the ECMAScript spec,
but within the de-facto standard of common browser behavior
(AS LONG AS YOUR KEYS DON"T PARSE AS INTEGERS!).

On 09/30/2015 01:46 AM, Tatsujin wrote:

Currently the "items" container in the menu definition is of type |Object|.

This is risky (at best), as the property order is not guaranteed (see: http://stackoverflow.com/a/5525820/1111580)

Instead, a better type would be a list, e.g. storing the items like so:

|selector: '.my-menu', items: [ { key: 'item1', name: 'Item 1' }, { ... }, ], |

Of course, |key| I got from the top of my head... probably something more relevant could be chosen.

— Reply to this email directly or view it on GitHub https://github.com/swisnl/jQuery-contextMenu/issues/283.

richpri avatar Sep 30 '15 15:09 richpri

As @richpri said, as long as you do not use integers it'll be fine. And if you do, it seems only chrome really resorts. I have seem this same problem in the past in some of our projects.

bbrala avatar Sep 30 '15 15:09 bbrala

That may be so, but relying on the fact that most(?) browsers implement it a certain way is risky, at best.

That being said, I think it is mainly unintuitive; the items container is logically a listing (the order is important) not an association (key to information). Associative arrays/hashes/dictionary/map are conceptually non-ordered. I don't quite see what the benefit is from using an object for this?

Also, of course, changing it to list would be a non-breaking change (different container type), but perhaps both would have to be maintained for a while.

neonrust avatar Oct 01 '15 06:10 neonrust

I agree with this issue.

In fact I would even go a little further and suggest that the keys be removed. I'm not sure what they provide to the user, and in my current setting, it actually is annoying to provide keys to the items as they are automatically generated.

Ptival avatar Jun 02 '16 18:06 Ptival

I would need to dive into the code to see what rammefications that would have. I do understand you feel they should be optional.

bbrala avatar Jun 03 '16 06:06 bbrala

Yeah no worries, it's very easy to get around anyway! :)

Ptival avatar Jun 03 '16 14:06 Ptival

Current behavior is super annoying when dynamically putting menu items together on server side. Proposed changes should be the new standard.

lofcz avatar Jun 19 '20 17:06 lofcz