0

I need to make some kind of form item factory with javascript. The idea is that you get the data from backend and produce the forms which need to have different event handler which is taking same argument but different contents.

const updateHandler = (dispatch, formName, section) => {

   return (value) => {
     //do some stuff with value
     //... this part is different for the each different form


     //call function that handles updating redux and url
     updateUrl(dispatch, formName, section, value, ...)
     //taking same argument
   }

}

I am wondering if I can memorize 3 shared argument for sub function so that i do not have to pass same thing again. I thought about currying or apply but i do not i can achieve that with currying or apply.

fiddlest
  • 1,262
  • 2
  • 17
  • 42
  • `function(dispatch, formName, section, value, ...)` is not a function call. And what is the `...` in there supposed to mean? – Tomalak Aug 02 '17 at 04:39
  • You can look for an abstract factory, which will give you your form and eventhandler based on the desired condition/identifier. – Anil Aug 02 '17 at 04:40
  • Just drop the parameters/arguments from the inner function and it will work out of the box, accessing the outer variables from the closure. – Bergi Aug 02 '17 at 07:05
  • Sorry I think i confused you guys. "function" should have been function call. If i do not pass the argument, it wont work since it has different scope @Bergi Tomalak – fiddlest Aug 02 '17 at 11:41
  • Oh ok. What exactly is the `...`? Will anything happen after `updateUrl`? Do you need any of `dispatch`, `formname` and `section` when doing stuff with `value`? – Bergi Aug 02 '17 at 17:07
  • ... means few more arguments and i need formname, value – fiddlest Aug 02 '17 at 19:53

0 Answers0