Note that there are some explanatory texts on larger screens.

plurals
  1. POImage data in knockout.js
    primarykey
    data
    text
    <p>Using knockout.js I'm trying to achieve what essentially is an image preloader and cacher. The point of the exercise is that the images I am loading take quite some time to download, in addition to being processed first on the server. Therefore my UI (which can browse between sets of images) aims to only download these sets when necessary in order to reduce load. Also, i wan't to provide an indicator to the user that something is going on during the long wait.</p> <p>That was the why, and heres the description of my current "how":</p> <p>1: I use jQuery .load in order to populate an array with actual image data:</p> <pre><code> images[doc.pages()[i].pagenumber() - 1] = $("&lt;img /&gt;").attr('src', path).load(function() {} </code></pre> <p>I use .load because it only updates the DOM once every network request has finished, allowing me to up a counter for every iteration within the code block to indicate to the user that the load is underway (e.g. loadCounter++;)</p> <p>2: Inside the .load callback i now do this:</p> <pre><code>if(loadedPages === doc.pages().length){ if(loadedPages === doc.pages().length){ for(var x = 0; x &lt; images.length; x++){ $("#" + doc.idname()).append(images[x]); } } } </code></pre> <p>What's happening here is that the images array contains actual ready to go HTML image DOM elements that can be injected directly into structure once all network requests are finished.</p> <p>That was my current how, and this is what i would like to achieve next:</p> <p>Instead of doing this i would like to populate a knockout observableArray with the same image data, and do a foreach binding on it, e.g:</p> <pre><code>&lt;div data-bind="foreach:images"&gt; &lt;img data-bind="what goes here?"&gt; &lt;/div&gt; </code></pre> <p>This example finaly arrives at my problem. The data-bind structure in knockout usually let's you bind to attributes, e.g. <code>&lt;img data-bind="attr:{src:path}</code>. However, doing this causes the images to get re-loaded every time the dom updates (the image data is removed on browse).</p> <p>So finally, the question distilled is: can i populate an observableArray with DOM image elements like in my example and iterate them with knockout.js? And if this is not doable or impractical, are there any other solutions for caching and iterating images, preferably with knockout?</p> <p>Thanks in advance, answers will be rewarded </p> <p>UPDATE</p> <p>Can the <code>&lt;object&gt;</code> tag be used? e.g. <code>&lt;object data-bind="attr:{data: $data.imageData}"&gt;&lt;/object&gt;</code>. I'm playing with it now, but i only get [object Object]. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload