Skip to content

Commit

Permalink
fix(composition): fixed failing composition if backing view-model doe…
Browse files Browse the repository at this point in the history
…s not return or export the constructor function. closes #4
  • Loading branch information
ckotzbauer committed Jun 15, 2016
1 parent 94bf90e commit 399dbca
Show file tree
Hide file tree
Showing 13 changed files with 245 additions and 61 deletions.
2 changes: 1 addition & 1 deletion dist/amd/aurelia-knockout.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ declare module 'aurelia-knockout' {
processInstruction(ctx?: any, instruction?: any): any;
buildCompositionSettings(value?: any, bindingContext?: any): any;
loadModule(moduleId?: any): any;
endsWith(s?: any, suffix?: any): any;
getViewModelInstance(moduleId?: any): any;
}
export class KnockoutBindable {
observerLocator: any;
Expand Down
42 changes: 34 additions & 8 deletions dist/amd/knockout-composition.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ define(['exports', 'knockout', 'aurelia-dependency-injection', 'aurelia-loader',
var activationData = void 0;

if (typeof value === 'string') {
if (this.endsWith(value, '.html')) {
if (endsWith(value, '.html')) {
view = value;
moduleId = value.substr(0, value.length - 5);
} else {
Expand Down Expand Up @@ -149,7 +149,7 @@ define(['exports', 'knockout', 'aurelia-dependency-injection', 'aurelia-loader',
var settings = { view: view, viewModel: viewModel, model: activationData };

if (!viewModel && moduleId) {
return this.loadModule(moduleId).then(function (modelInstance) {
return this.getViewModelInstance(moduleId).then(function (modelInstance) {
settings.viewModel = modelInstance;
return Promise.resolve(settings);
});
Expand All @@ -159,21 +159,47 @@ define(['exports', 'knockout', 'aurelia-dependency-injection', 'aurelia-loader',
};

KnockoutComposition.prototype.loadModule = function loadModule(moduleId) {
return this.loader.loadModule(moduleId);
};

KnockoutComposition.prototype.getViewModelInstance = function getViewModelInstance(moduleId) {
var _this3 = this;

return this.loader.loadModule(moduleId).then(function (result) {
var index = moduleId.lastIndexOf("/");
var fileName = moduleId.substr(index === -1 ? 0 : index + 1).toLowerCase();

return this.loadModule(moduleId).then(function (result) {
if (typeof result !== 'function') {
return result;
var constructorPropName = getMatchingProperty(result, fileName);

if (constructorPropName) {
result = result[constructorPropName];
} else {
return result;
}
}

return _this3.container.get(result);
});
};

KnockoutComposition.prototype.endsWith = function endsWith(s, suffix) {
return s.indexOf(suffix, s.length - suffix.length) !== -1;
};

return KnockoutComposition;
}()) || _class);


function endsWith(s, suffix) {
return s.indexOf(suffix, s.length - suffix.length) !== -1;
}

function getMatchingProperty(result, propName) {
var properties = Object.keys(result);
for (var index = 0; index < properties.length; index++) {
var prop = properties[index].toLowerCase();
if (prop.indexOf(propName) !== -1) {
return properties[index];
}
}

return null;
}
});
2 changes: 1 addition & 1 deletion dist/aurelia-knockout.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ declare module 'aurelia-knockout' {
processInstruction(ctx?: any, instruction?: any): any;
buildCompositionSettings(value?: any, bindingContext?: any): any;
loadModule(moduleId?: any): any;
endsWith(s?: any, suffix?: any): any;
getViewModelInstance(moduleId?: any): any;
}
export class KnockoutBindable {
observerLocator: any;
Expand Down
42 changes: 35 additions & 7 deletions dist/aurelia-knockout.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class KnockoutComposition {
// See http://durandaljs.com/documentation/Using-Composition.html

if (typeof value === 'string') {
if (this.endsWith(value, '.html')) {
if (endsWith(value, '.html')) {
// The name of the html view (assuming that the model name is equivalent to the view)
view = value;
moduleId = value.substr(0, value.length - 5);
Expand Down Expand Up @@ -121,7 +121,7 @@ export class KnockoutComposition {
let settings = { view: view, viewModel: viewModel, model: activationData };

if (!viewModel && moduleId) {
return this.loadModule(moduleId).then((modelInstance) => {
return this.getViewModelInstance(moduleId).then((modelInstance) => {
settings.viewModel = modelInstance;
return Promise.resolve(settings);
});
Expand All @@ -131,19 +131,47 @@ export class KnockoutComposition {
}

loadModule(moduleId) {
return this.loader.loadModule(moduleId).then((result) => {
return this.loader.loadModule(moduleId);
}

getViewModelInstance(moduleId) {
let index = moduleId.lastIndexOf("/");
let fileName = moduleId.substr(index === -1 ? 0 : index + 1).toLowerCase();

return this.loadModule(moduleId).then((result) => {
if (typeof result !== 'function') {
//result = result[Object.keys(result)[0]];
return result;
// Try to find a property which name matches the filename of the module
let constructorPropName = getMatchingProperty(result, fileName);

if (constructorPropName) {
// Use function of property.
// This occurs if the constructor function is exported by the module.
result = result[constructorPropName];
} else {
// The module returns an instance.
return result;
}
}

return this.container.get(result);
});
}
}

endsWith(s, suffix) {
return s.indexOf(suffix, s.length - suffix.length) !== -1;
function endsWith(s, suffix) {
return s.indexOf(suffix, s.length - suffix.length) !== -1;
}

function getMatchingProperty(result, propName) {
let properties = Object.keys(result);
for (let index = 0; index < properties.length; index++) {
let prop = properties[index].toLowerCase();
if (prop.indexOf(propName) !== -1) {
return properties[index];
}
}

return null;
}

@inject(ObserverLocator)
Expand Down
2 changes: 1 addition & 1 deletion dist/commonjs/aurelia-knockout.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ declare module 'aurelia-knockout' {
processInstruction(ctx?: any, instruction?: any): any;
buildCompositionSettings(value?: any, bindingContext?: any): any;
loadModule(moduleId?: any): any;
endsWith(s?: any, suffix?: any): any;
getViewModelInstance(moduleId?: any): any;
}
export class KnockoutBindable {
observerLocator: any;
Expand Down
44 changes: 35 additions & 9 deletions dist/commonjs/knockout-composition.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ var KnockoutComposition = exports.KnockoutComposition = (_dec = (0, _aureliaDepe
var activationData = void 0;

if (typeof value === 'string') {
if (this.endsWith(value, '.html')) {
if (endsWith(value, '.html')) {
view = value;
moduleId = value.substr(0, value.length - 5);
} else {
Expand Down Expand Up @@ -133,7 +133,7 @@ var KnockoutComposition = exports.KnockoutComposition = (_dec = (0, _aureliaDepe
var settings = { view: view, viewModel: viewModel, model: activationData };

if (!viewModel && moduleId) {
return this.loadModule(moduleId).then(function (modelInstance) {
return this.getViewModelInstance(moduleId).then(function (modelInstance) {
settings.viewModel = modelInstance;
return Promise.resolve(settings);
});
Expand All @@ -143,20 +143,46 @@ var KnockoutComposition = exports.KnockoutComposition = (_dec = (0, _aureliaDepe
};

KnockoutComposition.prototype.loadModule = function loadModule(moduleId) {
return this.loader.loadModule(moduleId);
};

KnockoutComposition.prototype.getViewModelInstance = function getViewModelInstance(moduleId) {
var _this3 = this;

return this.loader.loadModule(moduleId).then(function (result) {
var index = moduleId.lastIndexOf("/");
var fileName = moduleId.substr(index === -1 ? 0 : index + 1).toLowerCase();

return this.loadModule(moduleId).then(function (result) {
if (typeof result !== 'function') {
return result;
var constructorPropName = getMatchingProperty(result, fileName);

if (constructorPropName) {
result = result[constructorPropName];
} else {
return result;
}
}

return _this3.container.get(result);
});
};

KnockoutComposition.prototype.endsWith = function endsWith(s, suffix) {
return s.indexOf(suffix, s.length - suffix.length) !== -1;
};

return KnockoutComposition;
}()) || _class);
}()) || _class);


function endsWith(s, suffix) {
return s.indexOf(suffix, s.length - suffix.length) !== -1;
}

function getMatchingProperty(result, propName) {
var properties = Object.keys(result);
for (var index = 0; index < properties.length; index++) {
var prop = properties[index].toLowerCase();
if (prop.indexOf(propName) !== -1) {
return properties[index];
}
}

return null;
}
2 changes: 1 addition & 1 deletion dist/es2015/aurelia-knockout.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ declare module 'aurelia-knockout' {
processInstruction(ctx?: any, instruction?: any): any;
buildCompositionSettings(value?: any, bindingContext?: any): any;
loadModule(moduleId?: any): any;
endsWith(s?: any, suffix?: any): any;
getViewModelInstance(moduleId?: any): any;
}
export class KnockoutBindable {
observerLocator: any;
Expand Down
41 changes: 33 additions & 8 deletions dist/es2015/knockout-composition.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export let KnockoutComposition = (_dec = inject(CompositionEngine, Container, Lo
let activationData;

if (typeof value === 'string') {
if (this.endsWith(value, '.html')) {
if (endsWith(value, '.html')) {
view = value;
moduleId = value.substr(0, value.length - 5);
} else {
Expand Down Expand Up @@ -110,7 +110,7 @@ export let KnockoutComposition = (_dec = inject(CompositionEngine, Container, Lo
let settings = { view: view, viewModel: viewModel, model: activationData };

if (!viewModel && moduleId) {
return this.loadModule(moduleId).then(modelInstance => {
return this.getViewModelInstance(moduleId).then(modelInstance => {
settings.viewModel = modelInstance;
return Promise.resolve(settings);
});
Expand All @@ -120,16 +120,41 @@ export let KnockoutComposition = (_dec = inject(CompositionEngine, Container, Lo
}

loadModule(moduleId) {
return this.loader.loadModule(moduleId).then(result => {
return this.loader.loadModule(moduleId);
}

getViewModelInstance(moduleId) {
let index = moduleId.lastIndexOf("/");
let fileName = moduleId.substr(index === -1 ? 0 : index + 1).toLowerCase();

return this.loadModule(moduleId).then(result => {
if (typeof result !== 'function') {
return result;
let constructorPropName = getMatchingProperty(result, fileName);

if (constructorPropName) {
result = result[constructorPropName];
} else {
return result;
}
}

return this.container.get(result);
});
}

endsWith(s, suffix) {
return s.indexOf(suffix, s.length - suffix.length) !== -1;
}) || _class);

function endsWith(s, suffix) {
return s.indexOf(suffix, s.length - suffix.length) !== -1;
}

function getMatchingProperty(result, propName) {
let properties = Object.keys(result);
for (let index = 0; index < properties.length; index++) {
let prop = properties[index].toLowerCase();
if (prop.indexOf(propName) !== -1) {
return properties[index];
}
}
}) || _class);

return null;
}
2 changes: 1 addition & 1 deletion dist/system/aurelia-knockout.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ declare module 'aurelia-knockout' {
processInstruction(ctx?: any, instruction?: any): any;
buildCompositionSettings(value?: any, bindingContext?: any): any;
loadModule(moduleId?: any): any;
endsWith(s?: any, suffix?: any): any;
getViewModelInstance(moduleId?: any): any;
}
export class KnockoutBindable {
observerLocator: any;
Expand Down
Loading

0 comments on commit 399dbca

Please sign in to comment.