kickout.js afterRender如何工作? [英] How does knockout.js afterRender work?

查看:139
本文介绍了kickout.js afterRender如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对我来说,knockout.js的afterRender文件尚不清楚. 它的目的是什么?我们必须如何使用它? 一些主要用法示例是什么?

The documentation of afterRender of knockout.js is not clear to me. What is the purpose of it and how do we have to use it? What are some main usage examples?

推荐答案

击倒 afterRender ,无论是在首次初始化foreach还是在首次新建时条目稍后添加到关联的数组中.如果在使用foreach时涉及大量逻辑,则这可能会很昂贵.

Knockout afterRender is invoked each time the foreach block is duplicated and inserted into the document, both when foreach first initializes, and when new entries are added to the associated array later. This can be costly when there is heavy logic involved while using a foreach.

afterAdd —与 afterRender 相似,只是它仅在将新条目添加到数组中时调用(而不是在foreach首先迭代数组的初始内容时调用). afterAdd 的常见用法是调用诸如jQuery的$(domNode).fadeIn()之类的方法,以便在添加项目时获得动画过渡.

afterAdd — is like afterRender, except it is invoked only when new entries are added to your array (and not when foreach first iterates over your array’s initial contents). A common use for afterAdd is to call a method such as jQuery’s $(domNode).fadeIn() so that you get animated transitions whenever items are added.

如果只需要在将"NEW"项添加到阵列后运行代码,则使用 afterAdd .

Use afterAdd if you only need code to run after a 'NEW' item had been added to the array.

为避免在将每个项目添加到UI后这种昂贵的运行代码形式,请创建一个自定义绑定处理程序以在页面加载后运行.

To avoid this expensive form of running code after each item is added to the UI, create a custom binding handler to run after the page has been loaded.

下面是我用来隐藏pageLoad上的表单的一个.当然,您可以通过初始化和更新选项告诉它执行您想要的操作.

Below is one I used to hide the form on pageLoad. Of course you can tell it to do what you want with options for initialization and update.

示例代码:

ko.bindingHandlers.pageLoad = {
init: function (element, valueAccessor, allBindingsAccessor, data, context) {
    var value = ko.unwrap(valueAccessor());
    $(element).hide(value);

},
update: function (element, valueAccessor, allBindings) {     
}};

还签出:之后删除之前移动之后移动 在所选元素上标记:

data-bind="pageLoad: function() { hideForm() }"

javascript代码:

The javascript code:

self.hideForm = function () {
        $("#form").hide("slide", 500);
};

这篇关于kickout.js afterRender如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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