Skip to content

Instantly share code, notes, and snippets.

@rileytg
Last active Jan 4, 2016
Embed
What would you like to do?

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

This comment has been minimized.

Copy link

@danielcomer danielcomer commented Jan 29, 2014

Two thumbs up from me!!

@awellsndt

This comment has been minimized.

Copy link

@awellsndt awellsndt commented Jan 29, 2014

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

This comment has been minimized.

Copy link

@danielcomer danielcomer commented Jan 29, 2014

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

This comment has been minimized.

Copy link
Owner Author

@rileytg 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