Skip to content

Instantly share code, notes, and snippets.

@TechplexEngineer
Created December 19, 2011 05:25
Show Gist options
  • Save TechplexEngineer/1495529 to your computer and use it in GitHub Desktop.
Save TechplexEngineer/1495529 to your computer and use it in GitHub Desktop.
Is there a way to make this work?
var q = $.parseQuery();
if(!q.plugin)//If the value isn't set
window.location = "./"; //Go to the index file
yepnope([{
test: q.plugin === 'javascript',
yep: 'plugins/javascript.js',
nope: yepnope([{
test: q.plugin === 'arduino',
yep: 'plugins/arduino.js',
nope: yepnope([{
test: q.plugin === 'frcjava',
yep: 'plugins/frcjava.js',
nope: function() {alert("error");} //What if none of the tests are true.
}])
}])
}]);
//In theory this will run the tests and if none of them are true, then It should call the alert()
@SlexAxton
Copy link

Since you are checking against mutually exclusive things, you don't have to nest at all here. I think what you are looking for is the else if case, and we don't really like that pattern for this. Luckily, you don't need it:

var any = false,
     checkAll = function ( url, res, key){
       if ( res ) {
         any = true;
       }
     };

yepnope([{
  test: q.plugin === 'javascript',
  yep: 'plugins/javascript.js',
  callback : checkAll
},
{
  test: q.plugin === 'arduino',
  yep: 'plugins/arduino.js',
  callback : checkAll
},
{
  test: q.plugin === 'frcjava',
  yep: 'plugins/frcjava.js',
  callback : checkAll,
  complete : function () {
    if ( ! any ) {
      alert( 'error' );
    }
  }
}]);

@SlexAxton
Copy link

Just thinking of an even sexier way to solve this would be:

var testRes = ({
  "javascript" : "plugins/javascript.js",
  "arduino" : "plugins/adruino.js",
  "frcjava" : "plugins/frcjava.js"
})[ q.plugin ];

yepnope({
  test : testRes,
  yep : testRes,
  callback : function () {
    ! testRes && alert('error');
  }
});

@TechplexEngineer
Copy link
Author

Thanks!
I can't seem to get this second solution to work.
The first works beautifully though.

@SlexAxton
Copy link

Ah, try complete instead:

var testRes = ({
  "javascript" : "plugins/javascript.js",
  "arduino" : "plugins/adruino.js",
  "frcjava" : "plugins/frcjava.js"
})[ q.plugin ];

yepnope({
  test : testRes,
  yep : testRes,
  complete : function () {
    ! testRes && alert('error');
  }
});

@TechplexEngineer
Copy link
Author

TechplexEngineer commented Dec 22, 2011 via email

@SlexAxton
Copy link

Just wanted to close this thread with a quick "boo-yah" cause I'm in that kind of mood. Programming is fun.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment