javascript 使用knockoutjs进行加载绑定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10194929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:59:33  来源:igfitidea点击:

Onload Binding using knockoutjs?

javascriptdata-bindingbindingknockout.js

提问by BlaShadow

I Want to execute a function when the page load but don't know how to do it. cause that I have a list but his data from to another place in the load.

我想在页面加载时执行一个函数,但不知道该怎么做。因为我有一个列表,但他的数据从负载中的另一个地方。

回答by SkiReports.com

It sounds like you are refreshing page content through an AJAX call and you want to make that AJAX call on the page load in ADDITION to binding it to an event.

听起来您正在通过 AJAX 调用刷新页面内容,并且您希望在 ADDITION 中加载页面时进行该 AJAX 调用以将其绑定到事件。

Assuming that is the case, at the end of your ModelViewModel declaration, simply call the function. For example:

假设是这种情况,在 ModelViewModel 声明的末尾,只需调用该函数。例如:

function SearchResultsViewModel(){
    this.updateResults=function(){
      //Some AJAX Call and action.   
    }
    this.updateResults();
}

回答by Jason More

You bind the knockout viewmodel to the view as normal, then use

您像往常一样将淘汰赛视图模型绑定到视图,然后使用

window.onload = function ()
{
     //data from to another place in the load
}

or in jQuery

或在 jQuery 中

$(document).ready(function(){
     //data from to another place in the load
)};

Since knockout is bound to the view, when you add items, the view will update automatically.

由于knockout 绑定到视图,当您添加项目时,视图将自动更新。