使用javascript加载基因敲除js组件 [英] Load knockoutjs component using javascript

查看:90
本文介绍了使用javascript加载基因敲除js组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用JavaScript代码加载ko组件而不是在html中定义自定义元素的最佳方法是什么?我尝试使用ko.components.defaultLoader.load,但是我的组件构造函数没有命中. 我仔细检查过,该组件似乎已注册.

What's the best way to load a ko component with JavaScript code instead of defining a custom element in html? I tried with ko.components.defaultLoader.load but my component constructor does not hit. I double checked and the component appears to be registered.

推荐答案

我相信您正在寻找的是功能ko.components.get(componentName,callback).此方法的作用是要求组件加载程序解析组件名称,直到找到一个为止.如果找不到,它将调用callback(null).如果一切正常,它将调用callback(componentDefinition),其中componentDefinition是用于注册组件的对象,例如{viewmodel:...,template:...}.

I believe what you are looking for is function ko.components.get(componentName, callback). What this method does is ask the component loaders to resolve the component name until it finds one. If it doesn't find one, it will call callback(null). If it does fine one, it will call callback(componentDefinition), where componentDefinition is the object used to register the component, like { viewmodel: ..., template: ...}.

据我所知,没有现成的函数可以返回工作中"的组件.获取componentDefinition对象后要做的事情是这样的:

As far as I can tell, there isn't a ready made function which returns a "working" component. What you have to do after getting the componentDefinition object is something like:

  1. 将模板转换为DOM元素
  2. 实例化视图模型(如果已定义)
  3. 将viewmodel绑定到DOM元素

请注意,这并非一帆风顺,因为可以通过多种方式定义模板和视图模型. 我建议查看 https://github.com/knockout/knockout /blob/master/src/components/componentBinding.js ,并在此处查看操作方法(从第38行开始).

Note that this is not straight away because templates and view models can be defined in several ways. I recommend looking at https://github.com/knockout/knockout/blob/master/src/components/componentBinding.js and see how it's done here (from line 38).

我希望这对您有用,否则您可以考虑其他选择,例如在具有组件绑定的代码中动态创建div元素,其中组件名称和参数绑定到视图模型的属性.然后将此视图模型绑定到刚创建的div元素.这应该在仅代码"下工作,该代码比其他路由少得多.

I hope this works for you, otherwise you could consider other options, like dynamically creating a div element in code with a component binding where the component name and parameters are bound to properties of a view model. Then bind this view model to the div element you just created. This should work "code only" which much less code than the other route.

这篇关于使用javascript加载基因敲除js组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆