Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show an image loading spinner with knockout.js

I'm using knockout to bind a list of images. What's the best way to set up a spinner background while the images are loading. I have a spinner class I can set and unset to the background image, but wondering if there is an easy way to bind to image complete events with knockout.js.

like image 351
MonkeyBonkey Avatar asked May 23 '12 20:05

MonkeyBonkey


1 Answers

using jquery UIs little spinner thing, I have a binding handler like

 ko.bindingHandlers.Loading = {
        update: function (element, valueAccessor, allBindingsAccessor) {
            var value = valueAccessor(), allBindings = allBindingsAccessor();
            var valueUnwrapped = ko.utils.unwrapObservable(value);

            if (valueUnwrapped == true)
                $(element).showLoading(); // Make the element visible
            else
                $(element).hideLoading();   // Make the element invisible
        }
    };

and then use it like

<div data-bind="Loading: isLoading" >

so, basically, you can bind it to anything on your view model that might represent its loading ( or busy ) or not.

like image 121
Keith Nicholas Avatar answered Nov 13 '22 06:11

Keith Nicholas