Annyang command uses a *val to input values to the callback function. What should I do if I want to input more than one item ?
For example, I want to update a form based on user input. So I could have a command like
set Name *val
then val would contain the Name value that I could then assign to the field. But if my form has lot of fields writing a command like this for all fields could be tedious. Instead, I could have
set *FieldName *FieldVal
This command, would return two parameters instead FieldName and FieldVal. If FieldName is valid name of available fields on the page, then I could update the value with FieldVal, or ignore the command.
One way to achieve this, is, ofcourse parse the val to first get the FieldName and then FieldVal from a command like
set *val
but I dont think it would always result in clean solutions.
is there a suggested method to achieve this ?
What you are looking for is not the "splat" operator (*
) but the "named-variable" operator (:
).
Your command will then look like
var commands = {
'set :FormId with :Value':setForms,
}
annyang.addCommands(commands);
function setForm(formId, value){
if(isValidForm(formId)){
forms[formId].value = value;
}
}
And now saying "Set foo with bar" will set the forms["foo"]'s value to "bar".