I am working on a form builder application. I need to store the events for the particular element into the database at design level and retrieve the events when the form is in published state.
I am storing the events as follows and this is example with more than one event for a control:
Structure of saving events in Database for one element:
{
Event:
{
EventType: "click",
EventHandler: "var i = 0; alert(i);"
},
Event:
{
EventType: "change",
EventHandler: "var i = 0; alert(i);"
}
}
Example HTML Code:
I am giving an example for textbox
control in a form builder.
<input type="text" id="text1" />
On publishing the built form, I need to bind the above events for the built form which contains only one textbox.
Current Approach:
What I did is, I used eval()
to execute the code and my code is as follows:
function BindEventsForControl(controlId,eventType,eventHandlerValue){
//Contruct the event string
var scriptString = "$(document).on('"+ eventType +"',"'+ controlId +"',function(){"+ eventHandlerValue +"});"
//Evaluate the above expression
eval(scriptString);
}
The above method works perfectly.
My concern:
I need to call BindEventsForControl()
for each control in a form and need to use eval()
many times. Is this feasible ?
My question:
Is my approach feasible ? Is there any better approach out there? Again, I am mentioning I want to execute the events stored as string.
If you know anyother new method, then kindly help me with that.
Also, I am using Knockout.js in my application. Is there any way to approach with knockout.js for these kind of scenario ?