Knockout.js component callback after all items are loaded in foreach


Cenang

I have a component in kickoutjs that creates a list of albums from facebook using javascript graph api.

How to call a function after all items are rendered?

I tried foreach: {data: items, afterRender: afterItemsLoaded}- not calling the method afterItemsLoadedfor each row after all items are rendered .

I try to use the following code:

HTML:

<h1>List: </h1>
<list></list>

Javascript:

var countOfItemLoaded = 0;

ko.components.register('list', {
    viewModel: function() {
    },
    template: '<div data-bind="foreach: {data: items, afterRender: afterItemsLoaded}"><div data-bind="text: $data"></div></div>'
});

var viewModel = function() {
    var self = this;
    self.items = ko.observableArray([]);

    $.getJSON("https://graph.facebook.com/410564482402231/albums?fields=photos,name,photos.name",  function (result) {   
            $.each(result.data,function(key, item)  {            
                self.items.push(item.name);
            });

    });


    self.afterItemsLoaded = function() {
        countOfItemLoaded = countOfItemLoaded + 1;
        alert("Loaded item " + countOfItemLoaded);
    }
};

ko.applyBindings(viewModel);

Demo in jsFiddle:

jsFiddle link

Dear

I don't think support has been added for this yet.

See also : https://github.com/knockout/knockout/pull/339

Ryan Niemeyer who worked on Knockout did provide this solution to others about 2 years ago , knockoutJS executes the callback after the foreach finishes rendering

Related


Callback after all async forEach callbacks complete

Andreasson: as the title suggests. What should I do? I want to call whenAllDone()after the forEach loop goes through each element and does some async processing . [1, 2, 3].forEach( function(item, index, array, done) { asyncFunction(item, function itemD

Javascript - Callback after all nested forEach loops are done

Reza Karami I'm sure this is a fairly simple task, but at the moment I can't figure it out. I have a set of nested forEach loops and need a callback when all loops have finished running. I am willing to use async.js This is what I'm using: const scanFiles = fu

Update background after all items are loaded

quark My problem is that my web application loads large images as background. It is applied in <style>the label at the top of the bodylabel. It works, but when opening the page, it shows a white background when the image is loaded. I wanted to be able to set t

Callback after all async forEach callbacks complete

Andreasson: as the title suggests. What should I do? I want to call whenAllDone()after the forEach loop goes through each element and does some async processing . [1, 2, 3].forEach( function(item, index, array, done) { asyncFunction(item, function itemD

Knockout.js foreach not showing list of items

Ronald McDonald this doesn't work <div data-bind="foreach:nonAdminIB"> <div><span data-bind="text: explanation"></span>&nbsp;</div> </div> This does show the data, but I've hardcoded the index number and I d

Knockout.js callback whenever a component is loaded into the DOM

Havardu I am writing a single page application using kickout.js. I want all textboxes in the application to behave in a certain way, for example, to select all the text currently in the set. To avoid duplicate solutions like adding custom bindings to all input

Angular - call function after all JS is loaded

User 1857116 I want to call a function when all files (JS) are loaded (because I want to call a function inside one of the files). How do I do this? .state('dashboard', { url: "/dashboard.html", templateUrl: "views/dashboard.html",

Angular - call function after all JS is loaded

User 1857116 I want to call a function when all files (JS) are loaded (because I want to call a function inside one of the files). How do I do this? .state('dashboard', { url: "/dashboard.html", templateUrl: "views/dashboard.html",

Update background after all items are loaded

quark My problem is that my web application loads large images as background. It is applied in <style>the label at the top of the bodylabel. It works, but when opening the page, it shows a white background when the image is loaded. I wanted to be able to set t

Callback after all async forEach callbacks complete

Andreasson: as the title suggests. What should I do? I want to call whenAllDone()after the forEach loop goes through each element and does some async processing . [1, 2, 3].forEach( function(item, index, array, done) { asyncFunction(item, function itemD

Knockout.js callback whenever a component is loaded into the DOM

Havardu I am writing a single page application using kickout.js. I want all textboxes in the application to behave in a certain way, for example, to select all the text currently in the set. To avoid duplicate solutions like adding custom bindings to all input

Angular - call function after all JS is loaded

User 1857116 I want to call a function when all files (JS) are loaded (because I want to call a function inside one of the files). How do I do this? .state('dashboard', { url: "/dashboard.html", templateUrl: "views/dashboard.html",

Angular - call function after all JS is loaded

User 1857116 I want to call a function when all files (JS) are loaded (because I want to call a function inside one of the files). How do I do this? .state('dashboard', { url: "/dashboard.html", templateUrl: "views/dashboard.html",

Execute JS after all images are loaded?

Panthro Below is my js class. I need it to execute after all images are loaded, I understand I need to do this in the following way: $(window).load But how do I incorporate it into the class? this.mc = this.mc || {}; (function () { "use strict"; var

Knockout.js callback whenever a component is loaded into the DOM

Havardu I am writing a single page application using kickout.js. I want all textboxes in the application to behave in a certain way, for example, to select all the text currently in the set. To avoid duplicate solutions like adding custom bindings to all input