show loading div while Knockout computed function is running

Bryan Dellinger

a bit new to knockout just trying to figure out how to show a loading a div while a ko computed function is running. I'm not quite sure exactly what I need maybe I need to use knockout extenders?

Anywhere here is the fiddle.

http://jsfiddle.net/zf5k9rxq/10/

html

<input data-bind="value: val" />
<p><span data-bind="text: comp"></span>
</p>
<div data-bind="if: showloading">Loading...</div>

Javascript

function model() {
    var self = this;

    self.val = ko.observable("hello");
    self.showloading = ko.observable(true);

    this.comp = ko.computed(function () {
        //show loading
        this.showloading(true);

          // begin long running function
        var i = 0;
        var j = 0;
        while (i < 100000) {
            i++;
            j = 0;
            while (j < 80000) {
                j++;

            }
        }
        // end long running function

        //hide loading and return
        this.showloading(false);
        return this.val().toUpperCase();
    }, this);



}

var mymodel = new model();

$(document).ready(function () {
    ko.applyBindings(mymodel);
});
super cool

I'm not sure why you need Show/Hide div in computed may be its a mock up of ajax call i believe .

You can achieve it by something like this . Check the commented lines in below code to see minor changes I've made .

view:

<input data-bind="value: val" />
<p><span data-bind="text: compute,visible:!showloading()"></span></p> /*Toggling span visibility if loader is running*/
<div data-bind="if: showloading">Loading...</div>

viewModel:

function model() {
    var self = this;
    self.val = ko.observable("hello");
    self.showloading = ko.observable(true);
    self.compute = ko.observable();

    ko.computed(function () {
        var val = self.val(); //Creating a subscription 
        //show loading
        self.showloading(true);
        setTimeout(function () { //Delaying execution to show Loader
            self.showloading(false);
            self.compute(val ? val.toUpperCase() : val) //Assigning value to observable inside computed .
        }, 3000)
    });
}

$(document).ready(function () {
    ko.applyBindings(new model());
});

PS:You can make use of subscribe too if you want to avoid computed.

working sample goes here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Knockout js computed running balance infinite loops

From Dev

Hide div with specific class while page is loading then show again after load

From Dev

How to show 'in progress' while a FileResult function is running in MVC4

From Dev

Second computed function causes error in Knockout code

From Dev

Simple jQuery show modal and print a print function while selecting a div

From Dev

jQuery: trying to show loading gif while div is loading

From Dev

Show message while javascript is running

From Dev

knockout foreach computed value

From Dev

Knockout Computed Observable with parameters

From Dev

Knockout Computed with Parameter not updated

From Dev

Knockout writable computed observables

From Dev

Loading a div while @PostConstruct processing

From Dev

jQuery function running while loading page instead of clicking on element

From Dev

Knockout computed property not binding

From Dev

Asynchronous call inside a Knockout computed function

From Dev

Show loading gif while image is loading

From Dev

knockout observable array computed

From Dev

Generic function that show loading icon over any given div

From Dev

Data context for computed observable function in Knockout.js

From Dev

Knockout computed reset on select show/hide

From Dev

knockout computed array not updating when function updates observable array used in computed

From Dev

Simple jQuery show modal and print a print function while selecting a div

From Dev

Show message while javascript is running

From Dev

Show loading gif image while running client-side code

From Dev

Show Loading Gif While Full Image Loading

From Dev

Thread for show pics while a function is running

From Dev

data bind if does show div knockout js

From Dev

JQuery - How to show a spinner while loading content in a div?

From Dev

Running a function every time a computed observable changes Knockout

Related Related

  1. 1

    Knockout js computed running balance infinite loops

  2. 2

    Hide div with specific class while page is loading then show again after load

  3. 3

    How to show 'in progress' while a FileResult function is running in MVC4

  4. 4

    Second computed function causes error in Knockout code

  5. 5

    Simple jQuery show modal and print a print function while selecting a div

  6. 6

    jQuery: trying to show loading gif while div is loading

  7. 7

    Show message while javascript is running

  8. 8

    knockout foreach computed value

  9. 9

    Knockout Computed Observable with parameters

  10. 10

    Knockout Computed with Parameter not updated

  11. 11

    Knockout writable computed observables

  12. 12

    Loading a div while @PostConstruct processing

  13. 13

    jQuery function running while loading page instead of clicking on element

  14. 14

    Knockout computed property not binding

  15. 15

    Asynchronous call inside a Knockout computed function

  16. 16

    Show loading gif while image is loading

  17. 17

    knockout observable array computed

  18. 18

    Generic function that show loading icon over any given div

  19. 19

    Data context for computed observable function in Knockout.js

  20. 20

    Knockout computed reset on select show/hide

  21. 21

    knockout computed array not updating when function updates observable array used in computed

  22. 22

    Simple jQuery show modal and print a print function while selecting a div

  23. 23

    Show message while javascript is running

  24. 24

    Show loading gif image while running client-side code

  25. 25

    Show Loading Gif While Full Image Loading

  26. 26

    Thread for show pics while a function is running

  27. 27

    data bind if does show div knockout js

  28. 28

    JQuery - How to show a spinner while loading content in a div?

  29. 29

    Running a function every time a computed observable changes Knockout

HotTag

Archive