BlueSpire/Durandal — pull request #127 by kenbrubaker (2013-04-09 19:53:50+00:00)
Title: Added composition.getActivatableInstance to match router.getActivatableInstance. Helps support of TypeScript. Allows viewmodel to be class member of module.

Added extensibility point for getting activatable instance from required module for viewmodel during composition.

Added `composition.getActivatableInstance` to implement functionality in composition.compose.
Changed `composition.compose` to call factored `composition.getActivatableInstance` function.

`composition` now matches `router` functionality as mentioned in https://github.com/BlueSpire/Durandal/issues/58.

Override to support JavaScript:

```TypeScript
// any cast needed until pull request is merged and durandal.d.ts is updated.
(composition).getActivatableInstance = (element, settings, module ) => {
var instance;

var moduleName = settings.model.match(/[^\/]*$/)[0];

if (moduleName) {
moduleName = moduleName.substring(0, 1).toUpperCase() + moduleName.substring(1);
}

if (typeof module === 'function') {
instance = new module(element, settings);
} else if (typeof module [moduleName] === 'function') {
var ctor = module [moduleName];
instance = new ctor(element, settings);
instance.__moduleId__ = module.__moduleId__;
}
return instance || module;
}
```

Allows shell.ts with this content:

```TypeScript
///

import _router = module('durandal/plugins/router');
import system = module('durandal/system');
import logger = module('../services/logger')

export class Shell {
public activate() {
return this.boot();
}
public router: _router = _router;

public log(msg, data, showToast) {
logger.log(msg, data, system.getModuleId(Shell), showToast);
}

private boot() {
this.router.map([
{
url: 'home',
},
{
url: 'details'
}
]);

this.log('Hot Towel SPA Loaded!', null, true);

return this.router.activate('home');
}
}
```