You can extend a plugin to create the menu items from the remote server. The code looks like this:
(function($){
function init(target){
var opts = $.data(target, 'menulink').options;
var m = $('<div></div>').menu({
onClick: function(item){
opts.onClickMenu.call(target, item);
}
});
$(target).menubutton($.extend(opts, {
menu: m
}));
opts.loader.call(target, {}, function(data){
$.map(data, function(row){
m.menu('appendItem', {
id: row.code,
text: row.text
})
});
});
}
$.fn.menulink = function(options, param){
if (typeof options == 'string'){
var method = $.fn.menulink.methods[options];
if (method){
return method(this, param);
} else {
return this.menubutton(options, param);
}
}
options = options || {};
return this.each(function(){
var state = $.data(this, 'menulink');
if (state){
$.extend(state.options, options);
} else {
$.data(this, 'menulink', {
options: $.extend({}, $.fn.menulink.defaults, $.fn.menulink.parseOptions(this), options)
});
}
init(this);
})
};
$.fn.menulink.methods = {
options: function(jq){
return $.data(jq[0], 'menulink');
}
};
$.fn.menulink.parseOptions = function(target){
return $.extend({}, $.fn.menubutton.parseOptions(target), $.parser.parseOptions(target, [
]));
};
$.fn.menulink.defaults = $.extend({}, $.fn.menubutton.defaults, {
method: 'post',
loader: function(param,success,error){
var opts = $(this).menulink('options');
if (!opts.url) return false;
$.ajax({
type: opts.method,
url: opts.url,
data: param,
dataType: 'json',
success: function(data){
success(data);
},
error: function(){
error.apply(this, arguments);
}
});
},
onClickMenu(item){}
});
$.parser.plugins.push('menulink');
})(jQuery);
Usage example:
<a href="#" class="easyui-menulink" data-options="
// url: '...',
// emulate to load data from server
loader: function(param,success){
var menus = [{code:'c1',text:'MenuItem1'},{code:'c2',text:'MenuItem2'}];
success(menus)
},
onClickMenu(item){
console.log(item)
}
">MenuLink</a>