afterRender
适用于模板绑定,但在将模板转换为组件后,似乎无法使用afterRender
。我试图寻找使用afterRender
的组件的示例,但找不到任何内容。
答案 0 :(得分:36)
根据上面的帖子,我无法使用该方法。但是我在git问题列表上找到了一个解决方法,并且它不需要自定义KO绑定。
在组件模板html或代码字符串中添加以下行。
<span data-bind="template: { afterRender: init }"></span>
然后在module / viewModel中创建一个init函数:
this.init = function() {
Do cool DOM stuff here.
}
或取决于您的viewModel结构:
viewModel: function(params) {
return {
init: function () {
}
};
},
像魅力一样工作。它的工作示例在这里
http://jsfiddle.net/gLcfxkv6/1/
关于淘汰赛git的主题: https://github.com/knockout/knockout/issues/1533
感谢git上的vamps解决方法。
答案 1 :(得分:7)
这里的秘密是http://knockoutjs.com/documentation/custom-bindings.html
ko.bindingHandlers.myCustomBinding = {
init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
// This will be called when the binding is first applied to an element
// Set up any initial state, event handlers, etc. here
if (bindingContext.$data.init) bindingContext.$data.init(element, valueAccessor, allBindings, viewModel, bindingContext);
},
update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
// This will be called once when the binding is first applied to an element,
// and again whenever any observables/computeds that are accessed change
// Update the DOM element based on the supplied values here.
if (bindingContext.$data.update) bindingContext.$data.update(element, valueAccessor, allBindings, viewModel, bindingContext);
}
};
所以在我的组件模板中我做了类似
的事情<div class="row-fluid" data-bind="myCustomBinding: 'someValue'">
在组件viewModel上我只实现了init和/或update,例如:
constructor.prototype.init = function(element, valueAccessor, allBindings, viewModel, bindingContext) {
// All the buttons in the buttons group need the same name,
// but they all need distinct ids. We use timestamps because
// as components, the names and ids should be distinct across
// multiple instances of each component.
var timeStamp = new Date().getTime();
$('input:radio').attr('name', timeStamp).button();
$('input:radio:eq(0)').attr('id', timeStamp+1);
$('input:radio:eq(1)').attr('id', timeStamp+2);
$('input:radio:eq(2)').attr('id', timeStamp+3);
// Initialize the number-picker
$('input[name="number-picker"]').TouchSpin();
};
通过指出这个非常有用的案例,可以改进Knockout文档。此外,这是一个非常有用的绑定,应该有一个标准绑定&#39; init&#39;和&#39;更新&#39;,例如
<div data-bind="init: 'someValue'">
答案 2 :(得分:6)
在不同组件之间切换后,我们需要访问组件中的DOM元素。我们本来希望使用不存在的&#34; afterRender&#34;对组件的绑定。
我们用Javascript setTimeout解决了这个问题,让KO首先进行渲染,然后在此之后排队我们的代码。
HTML:
<div data-bind="component: compName"></div>
切换组件的代码:
var compName = ko.observable();
//...
compName(switchToComponent);
setTimeout(function(){
// this code is queued until after the component is rendered.
}, 0);
答案 3 :(得分:0)
像这样进行新的绑定
ko.bindingHandlers.initBinding = {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
debugger
if (valueAccessor() && valueAccessor().afterRender && bindingContext.$data) {
valueAccessor().afterRender(bindingContext.$data);
}
},
update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
}
};
export default ko.bindingHandlers.initBinding
如果不使用ES6(babel等),则不需要导出。
然后在组件html中,您可以执行以下操作
<div class="staff-directory" data-bind="initBinding: {afterRender: afterRender }">
<p>Loaded</p>
</div>
在您的组件模型中
class staffDirectory {
constructor() {
console.log('staff directory loaded');
}
afterRender() {
console.log('afterRender called');
}
}
export default staffDirectory;
答案 4 :(得分:0)
从敲除3.5.1开始,您可以向koDescendantsComplete
添加viewModel
函数,该函数将在渲染完成后触发
var viewModel = {
koDescendantsComplete: () => {
console.log( 'Rendered!' );
}
}