Skip to content

Instantly share code, notes, and snippets.

@rileytg
Last active January 4, 2016 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rileytg/8681920 to your computer and use it in GitHub Desktop.
Save rileytg/8681920 to your computer and use it in GitHub Desktop.

This function is intended to be a facade for requirejs's define

It provides a more descriptive name and creates a more vcs friends interface:

Compare (pseudocode): runWithDepedencies = function( dependencyNames, callable(depedencies) ) to define = function (name, deps, callback(dep1, dep2 etc) {

Consider (js):

define(['util', 'http', 'msg'], function (util, http, msg) {
  
}

Every time two people change dependencies, merge conflict.

var dependencies = [
  'util',
  'http',
  'msg'
];

define( dependencies, function (util, http, msg) {
  
}

Better, but still a merge conflict.

var dependencies = [
  'util',
  'http',
  'msg'
];

define( dependencies, function (
  util, 
  http, 
  msg
  ) {
  
}

No merge conflicts, but but ass ugly

var dependencies = [
  'util',
  'http',
  'msg'
];

runWithDependencies( dependencies, function (deps) {
  
  // example usage of the msg dependency
  deps.msg.someMethod();
  // or assign to variables
  var _ = deps.util;
  var http = deps.util;
   
}
  • Easy to scan and understand exactly what is happening
  • Easy (don't reassign to var) to locate dependency usages (desp.msg is likely a unique string despite it being a three letter name)
  • No merge conflicts
runWithDepedencies = (dependencyArray, callable) ->
define(depedencyArray, (dependencies...) ->
callable(dependecies)
)
var runWithDepedencies,
__slice = [].slice;
runWithDepedencies = function(dependencyArray, callable) {
return define(depedencyArray, function() {
var dependencies;
dependencies = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return callable(dependecies);
});
};
@danielcomer
Copy link

Two thumbs up from me!!

@awellsndt
Copy link

Just Curious, did you come up with this idea of having a runWithDependencies function or did you pick it up from somewhere. If you got it from a elsewhere I would like to read whatever commentary they had with it. I want to make sure that it doesn't interfere with the require minimizer.

@danielcomer
Copy link

Talked with alex. dependencies would be placed on global namespace. how about:

runWithDependencies([ 
  'util',
  'http',
  'msg'
], function (deps) {

  deps.msg.get('some.msg.key');
  // or assign to variables
  var _ = deps.util;
  var http = deps.util;

}

@rileytg
Copy link
Author

rileytg commented Jan 30, 2014

perfect.

@awellsndt I wrote it. Still needs to be adjusted for deps as an object not array and include tests. Not really my priority right now, ill come back to it when im feeling motivated

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