How to add event listener to dynamically created form
I am using the example here:
http://developer.appcelerator.com/blog/2011/10/forging-titanium-episode-10-forms.html/comment-page-1#comment-85110
To create a view/scrollview and add data collection to a window. On the window that receives the view I can't figure out how to add an event for one of my text field or pickers. I'd like to add the events/listeners to the window I am creating, not modify the forms.js file if possible.
Any suggestions? Thanks!
4 Answers
-
Accepted Answer
It looks like the approach you might have to take is go within the picker's "done" event listener. Maybe fire an app event inside of the picker's "done" event listener. Then you would need an app listener that would capture the id of the picker and it's value.
Inside of semiModalPicker.js you'll see this code near line 61:
done.addEventListener('click', function(e) { if (type === Ti.UI.PICKER_TYPE_DATE) { o.textField.value = dateToString(picker.value); } else { o.textField.value = picker.getSelectedRow(0).title; } modalWin.close(); });
I would modify it to be something like:
done.addEventListener('click', function(e) { if (type === Ti.UI.PICKER_TYPE_DATE) { o.textField.value = dateToString(picker.value); } else { o.textField.value = picker.getSelectedRow(0).title; } var picker_value = o.textField.value // gets the value based on the if/else outcome directly above Titanium.App.fireEvent(picker.id + '_updated', picker_value) // fires event for the picker named after the picker and passes in the value. modalWin.close(); });
So, if you call your picker in your var fields by using id:my_picker, you will create a custom fired event for that picker (and any other picker you create) called 'my_picker_updated', and pass in it's value. You could update a global value with it's listener or perform some logic based on which picker is fired. There are probably better approaches, this is just one.
I'm not able to test this, but it probably isn't too far off.
-
I use the example in the link in the OP to crate a form I want with a few pickers and a few text fields. For one of the text fields I want to populate it with info from my data base after the user makes a picker selection. So the user makes a picker selection and I want to look up some DB info and populate the text field below that picker.
I can't seem to figure out how to make an event and fire that event similar to the submit button in the example, but for one of the pickers. Would I need to modify the forms.js example to create the desired event at the time the picker is created? That seems to negate the benefits of a generic form creator.
-
Oops, sorry Rainer. This should have been a comment back to your comment not a new answer.
-
Not entirely sure what it is you want to do but this might be what you want. Add the event listener to your window, say you call it e. you can then do e.source to determine which field it came from.