Adding a loading icon to Knockout javascript

前端 未结 1 1146
刺人心
刺人心 2021-01-23 21:22

I have created a very simple example of a data load using Knockout. What I want to do is to add a a loading icon to show while the data is loading. Can anybody tell me the cor

1条回答
  •  情话喂你
    2021-01-23 22:13

    While this isn't your example specifically, this is a technique you could use to display a loading indicator of any sort:

    http://jsfiddle.net/wiredprairie/Uq8VJ/

    The important part is to just toggle the state of an observable in your view model, which then can trigger a visibility binding to hide or show a loading indicator.

    var vm = {
        isLoading: ko.observable(false),
        loadData: function () {
            var self = this;
            self.isLoading(true);
            $.getJSON("/echo/json?json={}&delay=2")
                .success(function () {
                // success!
            })
                .complete(function () {
                    // always remove the loading, regardless of load/failure
                self.isLoading(false);
            });
        }
    };
    
    ko.applyBindings(vm);
    

    And the HTML:

    always showing
    Loading...

0 讨论(0)
提交回复
热议问题