Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

what do these event listeners do? Why / How can you define a function inline?

Please excuse this basic question. This is a snipet of code from the Persistence example.

What do the two event listeners do?

Also, I see this all over, so it's clearly a part of coding I don't understand. Are you defining a function within that eventlistener definition? How does defining "e" in the function work?

Clearly I don't know enough about javascript. Any tutorials out there that would make sense? I find a million in google, but none that seem to apply well to what I'm doing here in appcelerator.

var tf1 = Titanium.UI.createTextField({
    value:resources.en_us.hello,
    width:250,
    height:40,
    top:10,
    borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED,
    autocorrect:false
});
tf1.addEventListener('return', function() {
    tf1.blur();
});
tf1.addEventListener('change', function(e) {
  resources.en_us.hello = e.value;
});
— asked November 21st 2010 by Nick Bodmer
  • blur
  • function
  • iphone
  • mobile
0 Comments

3 Answers

  • Event Listener listen for, well, events. An event, such as "return" or "change" are called whenever they happen. So "return" is called whenever the "return" key is hit on the iPhone, and "change" is called whenever the textField content changes (So whenever someone types something, or deletes something).

    E simply defines the event's properties. "e.value" means the value that is currently in the TextField. So for the example you posted, whatever is in the TextField will appear in the "resources.en_us.hello" label. It doesn't even have to be "e". I use "u" and "y" and stuff. It's just a variable for the event's properties.

    Make sense?

    Oh and a function is a function. You could do something like:

    function Example_Function() {
      tf1.blur();
    };
    
    tf1.addEventListener('return', Example_Function);
    

    But that's just a waste of time, unless it is something you should use a global function for.

    — answered November 21st 2010 by Colton Arabsky
    permalink
    0 Comments
  • Colton is correct - I would add that if you're looking to bone up on JavaScript in general, that you might want to check out Mozilla's guide - it's my go-to JS reference.

    — answered November 21st 2010 by Kevin Whinnery
    permalink
    1 Comment
    • Thank you for linking this. This was my go-to reference as well, but when I got a new computer I didn't transfer my Bookmarks and this one sort of got lost. Thanks Kevin! :)

      — commented November 21st 2010 by Colton Arabsky
  • Hello,
    I had the same problem as Nick. Answer from Colton is very helpfull, but to be sure to understand, can I go a little bit more in detil (or reformulation). (I'm a beginner too, more used to python than javascript and english is not so fluent, sorry in adavance).

    What causse my hairs problem is not only "e" but "function(e)". If I undertand well Colton answer and especially is example code:

    • The second argument of the addEventListener (method, function here ?) is the code to be executed when the event is fired. This argument could be "Example_function()" if "Example_fuctio()n" is defined BEFORE in the code. Or this argument can be literally "fuction() if it refers to code inside {} of the addEventListener (method/function) call ? I'm I right on this point ?

    • Now the "e" !! For the tf1.blur(), there is no "e" as arg of function. I suppose this is because tf1.blur() doesn't need any parameters to run ? In the other case, because the value need to be assigned to the label, then we need to catch the TextField value after change to assing ? Again, am I right on this point ?

    • "e" again !! Colon said that "E simply defines the event's properties". Then E contains value, width, top etc …. And od course e.value is the content of the TextField. Then is it wrong to say that E defines properties of the object on wich events are linked ?

    If you guys have time to give me answer on my question, it could make my brain less foggy !!!

    Thanks

    Alain

    — answered December 27th 2011 by Alain Abraham
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.