garlicjs-rails has a callback called ‘onRetrieve’. Im thinking thats my solution to highlighting which fields are from localStorage. I have an alert in here now just for debugging but would change this to add a class to those fields that are getting their data from localStorage (ie editing existing data where some fields are in the DB and some either are not or have been changed).
this script is from the garlicjs.js site
$( 'input.no_good' ).garlic( {
onRetrieve: function ( elem, retrievedValue ) {
console.log( 'The retrieved value for ' + elem.name() + ' is : ' + retrievedValue );
}
} );
this is what I am attempting to use
$("input#user_certificatenumber").garlic onRetrieve: (elem, retrievedValue) ->
alert "The retrieved value for " + elem.name() + " is : " + retrievedValue
Nothing is happening for me… What Im expecting is that if the element with id user_certificatenumber is retrieved from the localStorage that ‘.garlic onRetrieve’ will trigger and run the function. Not so.
I hoping I can get two things out of asking this question: Learn javascript a bit better and figure out how to watch all fields in a form, not just one, and then use js to add class and color up those inputs that were retrieved from the localStorage.
Did you add
data-persist="garlic"on your form ?If so, your
onRetrievelistener is not applied to your form elements, because form is already binded by data-persist.Just add your lines to the entire form (without data-persist) to bind Garlic on its elems AND register onRetrieve listener on each fields
Best