Skip to content

Commit 9780b66

Browse files
committed
Start docs, comment fix, add todo's for few tasks.
1 parent a5ec260 commit 9780b66

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
currentMenu: sub-menus-promise
3+
---
4+
5+
# Demo: Submenus with promise
6+
7+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
8+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
9+
10+
11+
- [Example code](#example-code)
12+
- [Example HTML](#example-html)
13+
14+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
15+
16+
<span class="context-menu-one btn btn-neutral">right click me</span>
17+
18+
## Example code
19+
20+
<script type="text/javascript" class="showcase">
21+
var errorItems = { "errorItem": { name: "Could not load items" },};//example usage if you want to reject promise
22+
var loadItems = function () {//example method that will eventually do an async call
23+
var dfd = jQuery.Deferred();
24+
setTimeout(function () {
25+
dfd.resolve(subItems);
26+
}, 1000);
27+
//setTimeout(function () {
28+
// dfd.reject(errorItems);
29+
//}, 1000); //could be used to reject items, providing the same format list.
30+
return dfd.promise();//return a jquery promise, see http://api.jquery.com/deferred.promise/
31+
};
32+
33+
var subItems = {
34+
"sub1": { name: "Submenu1", icon: "edit" },
35+
"sub2": { name: "Submenu2", icon: "cut" },
36+
};
37+
//normal context menu initialization.
38+
$.contextMenu({
39+
selector: '.context-menu-one',
40+
build: function ($trigger, e) {
41+
return {
42+
callback: function (key, options) {
43+
var m = "clicked: " + key;
44+
console.log(m);
45+
},
46+
items: {
47+
"edit": { name: "Edit", icon: "edit" },
48+
"cut": { name: "Cut", icon: "cut" },
49+
"status": {
50+
name: "Status",
51+
icon: "delete",
52+
items: loadItems(),//providing promise instead of items
53+
},
54+
}
55+
};
56+
}
57+
});
58+
</script>
59+
60+
## Example HTML
61+
<div style="display:none;" class="showcase" data-showcase-import=".context-menu-one"></div>

src/jquery.contextMenu.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,7 @@
12421242
// it later, after promise has been resolved.
12431243
if ('function' === typeof item.items.then) {
12441244
// probably a promise, process it, when completed it will create the sub menu's.
1245+
// @todo Add a loading class to the item so you know it is loading.
12451246
op.processPromises(item, root, item.items);
12461247
} else {
12471248
// normal submenu.
@@ -1446,12 +1447,14 @@
14461447
if (window.console) {
14471448
(console.error || console.log).call(console, 'When you reject a promise, provide an "items" object, equal to normal sub-menu items');
14481449
}
1449-
}else if(typeof errorItem === 'string'){
1450-
errorItem = { "error": { name: errorItem } };
1451-
}
1450+
} else if(typeof errorItem === 'string'){
1451+
errorItem = { "error": { name: errorItem } };
1452+
}
14521453
finishPromiseProcess(opt,root,errorItem);
14531454
};
14541455
function finishPromiseProcess(opt,root,items) {
1456+
// @todo Check here if this is even possible anymore, is the context menu still open.
1457+
14551458
opt.items = items; // Override promise to items.
14561459
op.create(opt, root, true); // Create submenu
14571460
op.update(opt, root); // Correctly update position if user is already hovered over menu item

0 commit comments

Comments
 (0)