Skip to content

Instantly share code, notes, and snippets.

@Jaben
Last active December 20, 2015 10:49
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 Jaben/6118234 to your computer and use it in GitHub Desktop.
Save Jaben/6118234 to your computer and use it in GitHub Desktop.
Patch to Extend Durandal's v2.0 handling of module exports (AMD) -- specifically make it work with TypeScript AMD exports.
/**
* Resolves the default object instance for a module.
* If the module is a function, that function is called with `new` and it's result is returned.
* If the module is an export object (AMD), a function named '[moduleId]' or '[moduleId]View' created with 'new' and returned.
* If the module is an export object (AMD), a instance named '[moduleId]' or '[moduleId]View' is returned.
* If the module is an object, the instance is returned.
* @method resolveObject
* @param {object} module The module to use to get/create the default object for.
* @return {object} The default object for the module.
*/
resolveObject: function(module) {
if (system.isFunction(module)) {
return new module();
} else {
var afterDash = function (str) {
var dashIndex = str.lastIndexOf("/", str.length - 1);
return str.substr(dashIndex + 1, str.length - dashIndex);
};
var findPropCaseInsensitive = function (obj, propName) {
propLower = propName.toLowerCase();
for (var prop in obj) {
if (prop.toLowerCase() === propLower) {
return prop;
}
}
return null;
}
var instance = module;
var moduleId = system.getModuleId(module);
var possibleExports = [afterDash(moduleId), afterDash(moduleId) + "View"];
for (var pi = 0; pi < possibleExports.length; pi++) {
var prop = findPropCaseInsensitive(module, possibleExports[pi]);
if (system.isFunction(module[prop])) {
instance = new module[prop]();
break;
}
else if (system.isObject(module[prop])) {
instance = module[prop];
break;
}
}
if (instance !== module) {
system.setModuleId(instance, moduleId);
}
return instance;
}
}
@Jaben
Copy link
Author

Jaben commented Jul 31, 2013

HOW TO USE:

Overwrite the resolveObject() function in the Durandal/system.js file with this function.

Now Durandal's will resolve by attempting to find the export of "moduleId" or "moduleIdView" and either create a new instance of the function or use the object instance.

@Jaben
Copy link
Author

Jaben commented Jul 31, 2013

An example for "viewmodel/summary.js" would be:

define(["require", "exports"], function(require, exports) {
    function SummaryView() {

    }
    SummaryView.prototypes.activate = function() {
       // do something...
       // return promise
    }
    // the modified 'resolveObject' above will locate this function
    // case without case sensitivity as either 'summary' or 'summaryview'
    exports.SummaryView = SummaryView;

    // OPTIONAL: use this object if the function export above was not available.
    exports.Summary = new SummaryView();
});

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