Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jQuery Custom Function: Improvements?

 
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
I've been trying something new (to me) lately while developing with jquery. I've been trying to write all my code as plugins or functions to jquery so that they become more reusable out of the box, so to speak. So even if the code is only being used in one place I write it so that it could be generally used elsewhere. I know, I know, everyone should be doing this and not just in JavaScript. Anyway...

I have a UL with dynamically added LI's. User types in some text into a text input and they click the Add button (or hit enter) and I add an LI with the text and a delete image. I wrote this little function so that the user passes in the text and I create the LI and I call a callback function the user specifies for the click() event of the image element.



And here is how it is used:



I'm just looking for some general improvement suggestions. It literally doesn't need to do more than it does right now. So adding functionality isn't really necessary.

Thanks.
[ August 24, 2008: Message edited by: Gregg Bolinger ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
I've been trying something new (to me) lately ...

This is very common in the jQuery community.

The rest looks fine to me, thought there are a bazillion different ways to do it. Personally, I tend to rely less on intermediate variables.
 
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
Originally posted by Bear Bibeault:
Personally, I tend to rely less on intermediate variables.


I assume you mean the variable creation for text_span, img_span, etc? If so, I thought about this. I for sure need the img instance so I can create the click event. I tried using less but it became a lot less readable. Do you have suggestions?
[ August 24, 2008: Message edited by: Gregg Bolinger ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More variables will improve readability.
 
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
Thanks. And just for completeness I now have a delete function that looks like this:



Using the add and delete functions, the actual implementation looks like this:



Man, I love jquery.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
Man, I love jquery.
For me, it's put all the fun back into writing client-side scripts. It's no longer a chore, but something I actually enjoy doing.
 
Uzbek Jon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg Bolinger wrote:

I'm just looking for some general improvement suggestions. It literally doesn't need to do more than it does right now. So adding functionality isn't really necessary.

Thanks.
[ August 24, 2008: Message edited by: Gregg Bolinger ]


Hi Gregg (and happy new year )

I came accross this thread through Google. There was another helpfull article on jquery custom functions. It suggests to hide your plugin code and make it more other javascript frameworks friendly. For example:

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uzbek Jon, please read this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic