0

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 ?

Vikash
  • 857
  • 1
  • 13
  • 32
  • Take a look at the html binding in knockout - http://knockoutjs.com/documentation/html-binding.html – gkb Oct 24 '16 at 07:29
  • Which is feasible ? KnockoutJs or my approach(which alreay mplemented) ? Let me know your thoughts.on this. – Vikash Oct 24 '16 at 17:35
  • Although using eval is considered a bad practice (take a look at this post http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea ), I don't know how good/bad it can be in your implementation...However, I feel if your are already using knockout, creating a string of html markups and using them in the html binding can be a better approach...as in this case you would be relying on tried, tested and more popular approach... – gkb Oct 25 '16 at 05:19

0 Answers0