wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes best approach to pass params to bound click callbacks. Take 2. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "best approach to pass params to bound click callbacks. Take 2." Watch "best approach to pass params to bound click callbacks. Take 2." New topic
Author

best approach to pass params to bound click callbacks. Take 2.

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

OK, I know this is probably a common question, and in my search for variable scoping, i have learned a lot, but this one thing is still bothering me as to what should be the best practice or approach.

I have a click event I am adding to an a link. When that event is fired the function that gets called has arguments/parameters that I need to pass. When I try it without an anonymous function, it seems to call the function right when binding the event to the link.

So two examples I have



In that code, which is working for me correctly calls the addItemToEvent function when the link is clicked.

I had tried



But that will call addItemToEvent when the click event is added.

I think I read somewhere I could do something like this. I think I typed it correctly, maybe a paran or curly missing somewhere or semi-colon.




I guess



is what really matters, and is only an extra line of code compared to my current solution. Or is it more code.

It would look like I would have to go through all my functions and declare them that way rather than the current approach of just



I guess I am almost seeing a benefit to that var and how I probably should not use my current approach. As those functions are added to the global scope document.functionName??? And instead create my own scope/namespace for my functions of my app? Then I could write my own library of functions that I could use in other applications. Like my confirm window/function.

Thanks

Mark rel="nofollow">


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Have you seen the section of the docs on passing data to the event? So you could do something like this:



If this isn't any where close to answering your question, please ignore.


GenRocket - Experts at Building Test Data
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Thunder stolen.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Just to say why this fails



It is saying execute the function addItemToEvent passing an argument of true and what ever it returns assign it to the 3 argument of the on call. It is NOT assigning a reference like you think it is.

Before jQuery allowed you to pass stuff with on, you would have had to use a closure.


Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Gregg Bolinger wrote:Have you seen the section of the docs on passing data to the event? So you could do something like this:



If this isn't any where close to answering your question, please ignore.


I have done that elsewhere in my code, but it didn't work in my case because of the .post() later on in the addItemToEvent function. I couldn't get that addItem value into the callback function of the .post();

If I added {addItem: true} to the .post so that I could get it in the callback function was not correct because that data would be sent to the server and come back only as a string of "addItem=true in the data.


But based on your replies, does that mean that doing

var addItemToEvent = function()…

is not a good idea?

Mark
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Mark Spritzler wrote:But based on your replies, does that mean that doing

var addItemToEvent = function()…

is not a good idea?

Not unless you want the result of running the function to be event handler.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Bear Bibeault wrote:
Mark Spritzler wrote:But based on your replies, does that mean that doing

var addItemToEvent = function()…

is not a good idea?

Not unless you want the result of running the function to be event handler.


So then you are saying that the cleanest best approach is to use {someVar: someData} approach? But what about using the same data/thing for the callback of a .post()?

Thanks

Mark
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

That's better handled with closures.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Bear Bibeault wrote:That's better handled with closures.


So then there are many ways to do it, and if you want to reuse that function in all the different ways you have to do the code each different way?

Some cases closures, some cases anonymous functions, some cases passing data via {}, and in other cases can call the function direct and pass params direct.

Wow, no wonder it is a mess. There is no consistency. But I will say if you have a handle on this, then you do it correct, but it is way to easy to do it wrong. You have a 25% chance you got it right, if you don't know what you are doing.

Thanks

Mark

p.s. I am a bit snippy because CITI is the devil and they should have been left to fail.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: best approach to pass params to bound click callbacks. Take 2.