我想使用命名约定创建自定义组件加载器,加载外部视图模型和视图文件。
var namingConventionLoader = {
getConfig: function(name, callback) {
// 1. Viewmodels are classes corresponding to the component name.
// e.g., my-component maps to MyApp.MyComponentViewModel
// 2. Templates are in elements whose ID is the component name
// plus '-template'.
var viewModelConfig = MyApp[toPascalCase(name) + 'ViewModel'],
templateConfig = { element: name + '-template' };
callback({ viewModel: viewModelConfig, template: templateConfig });
}
};
让我们说我加载视图模型构造函数及其视图html字符串async。如何正确地将它们提供给回调?
应该是
ko.components.defaultLoader.loadTemplate(name, ajaxHtmlString, function(template){
var theLoadedViewModel = function() {};
callback({ viewModel: theLoadedViewModel, template: template });
});