Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Wrapping jQuery Ajax Convenience Methods: Good idea or bad idea

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We're using jQuery's load() function a lot throughout our application. We've discovered, quite embarrassingly I might add, that we need better error handling. So instead of



we need to use



So that we can check the status and deal with things appropriately. I'm thinking of writing a wrapper around load() (among other things) that would allow us to write code like this (function name for demo only):



But as I look at that, it makes me think I should just use the $.ajax() function instead, since I can already do that. So what would be a better "standard"? Wrap the convenience methods or just fall back to .ajax() ?

 
Sheriff
Posts: 67583
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it's centralized error handling you are after, I'd just use the jQuery error events.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:If it's centralized error handling you are after, I'd just use the jQuery error events.



Yea, I'm already doing that for some things. However, these error handling bits aren't all generic. For example, in this one instance on a load() call, I am showing and hiding some elements on success. But if there was an error, I don't want to show/hide things. I need to basically not do anything.
 
Bear Bibeault
Sheriff
Posts: 67583
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I generally fall back to $.ajax in similar circumstances, unless it's something that will be done again and again and again, in which case extending/wrapping would be a decent approach.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:I generally fall back to $.ajax in similar circumstances, unless it's something that will be done again and again and again, in which case extending/wrapping would be a decent approach.



That's what I figured and just needed that confirmation. Thanks.
 
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic