Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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">
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thunder stolen.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's better handled with closures.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic