如何在使用组件时定义模板url
How to define a template url when using a component
我使用的是淘汰组件,但我想使用模板url而不是内联模板。这是我正在使用的组件:
ko.components.register('cat-data', {
viewModel: {
createViewModel: function(params, componentInfo) {
var self = this;
self.data = (params && params.data) || [];
return self;
}
},
template: "/Scripts/CatdDataTemplate.html"
});
当我运行此/Scripts/CatdDataTemplate.html
时,会显示它,而不是实际的模板。
通过敲除将字符串形式的模板属性解析为要应用的实际模板。如果你想从url加载模板,你可以使用自定义加载程序来完成,如下面引用的淘汰文档中所定义的:
如果您的自定义加载器实现loadTemplate和/或loadViewModel,然后您可以将自定义代码插入到加载过程中。你也可以使用这些函数可以解释自定义配置格式。
例如,您可能希望启用配置格式,如以下内容:
ko.components.register('my-component', {
template: { fromUrl: 'file.html', maxCacheAge: 1234 },
viewModel: { viaLoader: '/path/myvm.js' }
});
…并且您可以使用自定义加载程序来完成此操作。
以下自定义加载程序将负责加载模板使用fromUrl值配置:
var templateFromUrlLoader = {
loadTemplate: function(name, templateConfig, callback) {
if (templateConfig.fromUrl) {
// Uses jQuery's ajax facility to load the markup from a file
var fullUrl = '/templates/' + templateConfig.fromUrl + '?cacheAge=' + templateConfig.maxCacheAge;
$.get(fullUrl, function(markupString) {
// We need an array of DOM nodes, not a string.
// We can use the default loader to convert to the
// required format.
ko.components.defaultLoader.loadTemplate(name, markupString, callback);
});
} else {
// Unrecognized config format. Let another loader handle it.
callback(null);
}
} }; // Register it ko.components.loaders.unshift(templateFromUrlLoader);
来源:http://knockoutjs.com/documentation/component-loaders.html#example-2-组件加载-加载-外部文件-自定义代码
相关文章:
- LinkedIn共享链接图片未使用自定义url更新
- 在JQuery中将页面翻转转换为自定义url
- 不带“”的自定义URL;无法打开“;错误
- chrome.identity.launchWebAuthFlow中的自定义url
- 创建自定义URL
- 使用javascript中的变量创建自定义URL
- 具有动态定义url的iframe为空
- 自定义URL模式
- Facebook分享没有选择完整的网址&自定义url的元名称
- 如何使用带有jQuery和自定义url参数的cookie
- 在Facebook上共享自定义url的按钮
- 将自定义URL映射分配给JavaScript变量
- TypeError:这个_在Dojo FilteringSelect-onChange回调中未定义url
- 使用表单数据将用户带到自定义 URL
- 如何使用正则表达式验证 JavaScript 中的自定义 URL
- 未捕获的引用错误: 未定义 url
- 自定义 URL 协议可以调用 JavaScript 函数吗?
- 添加这需要共享 js 生成的自定义 url
- 使用 JavaScript 自定义 URL
- 如何在 Javascript 中定义 URL 以调用驻留在控制器中的 ActionResult 方法