Knockout.Js:执行顺序

时间:2018-04-06 21:43:40

标签: knockout.js

所以我尝试使用knockout为项目创建过滤器。过滤器的代码是:

<div class="filter">
    <input type="text" data-bind="textInput: filterText, event {keypress:filterEntries}" placeholder="Filter Locations">

在Knockout的文档中,它说要启用默认操作,我必须返回true,如下所示:

var ViewModel = function {
    this.filterText = ko.observable("");
    this.filterEntries = function(){
        console.log("filterEntries is being called");
        console.log(this.filterText());
        return true;
    };
}; 

现在我遇到的问题是我正在尝试使文本输入更新filterText然后调用函数filterEntries而不是相反。

1 个答案:

答案 0 :(得分:1)

keyup事件会在之后触发。

var ViewModel = function () {
        this.filterText = ko.observable("");
        this.filterEntries = function() {
            console.log("filterEntries is being called");
            return true;
        };
  
        this.filterText.subscribe(function(newValue) {
            console.log(newValue);
        });
    };

    ko.applyBindings(ViewModel);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<div class="filter">
        <input type="text" data-bind="textInput: filterText, event: 
    {keyup:filterEntries}" placeholder="Filter Locations">
    </div>