Garlic.js on dynamically generated fields
How can I get garlic.js to work with fields generated dynamically with Javascript like in this Railscast or using Cocoon ? I tried to explicitly include the js file in the nested fields, but no luck:
= javascript_include_tag 'garlic'
.nested-fields
= f.inputs do
= f.input :client_id #etc
source to share
The way I got around this, I used the jQuery store api: https://github.com/julien-maurel/jQuery-Storage-API to store the HTML of the dynamically generated form in addition to calling garlic to save the state of the form.
On page load, I check if the saved form exists in localstorage. If so, load the html into the form element and then call garlic to restore the form state!
source to share
The problem occurs when you restart or restart the browser, the request get
for edit/new
is made to the server, and hence the fields that are static in the corresponding view file are loaded with data and fields that were dynamically added. Hence, data will always be lost. I'm not sure if garglic.js will add data to fields that weren't in the DOM on page load.
source to share