Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript, function into a function

Tags:

javascript

I have some kind of error concept regarding to the creation of functions into functions, I would appreciate any help in here.

Let's say I have to apply the jquery $.post function several times, so I dont want to repeat myself, so I create something like this.

 myLoadFunction(url, function) {

     $.post(url, function( data ) {
        //something generic
     })
     .done(function(data) {
       //what I want to put on my caller function
     })
     .fail(function(data) {

       //something generic
     });

 }

so I can execute it like this:

myLoadFunction("myurlblabla", function() {
       //everything I need into the .done
});

Any ideas? I am not even sure if this is possible, so pls do not kill me :)

Regards!

like image 247
andresmijares Avatar asked Jul 30 '26 12:07

andresmijares


1 Answers

The structure you're trying to achieve is already there for you:

You want:

myLoadFunction("myurlblabla", function() {..if success...});

But You already have it in:

myLoadFunction("myurlblabla").done(...)  // nicer right ?

Using the deferred object, it already provides this:

Change the structure to return a deferred obj: ( or better see my edit)

myLoadFunction(url) 
{
    return $.post(url, function(data) {
        //something generic
    });
}

var R = myLoadFunction("myurlblabla");

R.done(function (){ do whatever}); // here is your actual question.
R.fail(function (){ do whatever});

Also don't mix responsibilites !

myLoadFunction should do load nothing more.

With the result of the load you can do other thing outside of this load factory function.

don't put logic there

edit

Regarding your comment :

but on this, would I allow to preserve the same answers for fail and the generic load? I just dont want to repeat the fail case nor the case when data is empty

You could set a function ( one time) which you will reference each time : ( don't put this on global scope. an object which contains these function would be great).

function myGENERICCallback(){ something I don't want to repeat}
function mydoneCallback(){ something I don't want to repeat}
function myfailCallback(){ something I don't want to repeat}
function myAlwaysCallback(){ something I don't want to repeat}

Now - you can do this :

var R = myLoadFunction("myurlblabla");

R.done(mydoneCallback); 
R.fail(myfailCallback);

And so your load function would look like :

myLoadFunction(url) 
{
    return $.post(url, myGENERICCallback); 
}
  • myGENERICCallback would always be called
  • you load function does not contain any logic
  • you can have specific callbacks for each case
  • you keep single responsibility principle.
  • you don't repeat your code
  • you only need to change in one place
  • Other can subscribe to your deferred also.
like image 188
Royi Namir Avatar answered Aug 01 '26 02:08

Royi Namir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!