I am using the code below to create a jQuery UI Dialog widget dynamically:
$(function () {
var Selector = $(\"a:contains(\'sometext\')\");
$(Sel
I needed a way to use a JSON webservice to control things like alerts and updates on the client-side without the client initiating an action. I hope to update this to use web-sockets, but for now it's a timed pull and each pull includes the delay for the next pull so I can even manage that once the client has loaded up my system.
I'm also using show/expire and moment.js to filter by datetime and then using cookies with the id of each alert (not shown here) to prevent duplicate notices. This is coming along nicely and I might just decide to package it up as a library before too long if I get enough interest.
The bit specific to this question is 2 parts though; 1) JSON that includes the parameters for the jQuery.dialog() and 2) The code to use that JSON and create a dialog. The key here is to pay attention to how I'm referencing the 'n' object parameters and using them to create the dialog dynamically. The tDlg object is a big part of that as it is what ultimately represents the dialog and is passed into $().dialog()
JSON Snippet that includes my dialog parameters:
"doAlert":{
"modal":false
,"height":240
,"width":380
,"title":"Welcome to the new BatchManager"
,"body":"Welcome to the new and enhanced
BatchManager!Enjoy!"
,"buttons":[
{
"text":"Ok"
,"click":"$(this).dialog('close');"
}
]
}
JavaScript snippet (using jQuery) to initialize a dialog based on my JSON (n corresponds with doAlert, which is part of an array of "notices" in this sample):
var tDlg = {
resizable: false,
height: (n.doAlert.height) ? n.doAlert.height : 240,
width: (n.doAlert.width) ? n.doAlert.width : 240,
modal: (n.doAlert.modal) ? n.doAlert.modal : true,
dialogClass: "dlgFont"
};
if (n.doAlert.buttons) {
tDlg.buttons = [];
$.each(n.doAlert.buttons, function (i, n) {
tDlg.buttons.push({
text: n.text,
click: new Function(n.click)
})
})
}
$('' + n.doAlert.body + '
').dialog(tDlg);