locked
Lazy/async WinJS.Binding.converter

    Question

  • I'd like to bind an <img> src to an image that is loaded or generated on-the-fly based on a property of an item in a ListView.

    I'm wondering if there is a way to implement an asynchronous data converter for a binding, or what other options I could use to accomplish this.

    Friday, February 03, 2012 1:07 PM

Answers

All replies

  • Hi G,

    Have you worked through the ListView quickstart?  That has a datasourse that specifies the src attribute that is used to load images.  If that is not what you want, please clarify.

    http://msdn.microsoft.com/en-us/library/windows/apps/hh465496.aspx

    -Jeff


    Jeff Sanders (MSFT)
    Friday, February 03, 2012 2:54 PM
    Moderator
  • Hi Jeff,

    I want exactly the same as G.

    I use a WinJs.BindingConverter to fetch a image (stream) from a WCF service and bind that against the src-attribute.

    But since fetching the data is done with a Promise I cannot return the result of the xhr-call in the converter method.

    <img class="article-image" src="#" id="my-image" data-win-bind="src: imageId Data.Converters.urlToContent; alt: title" />


    var urlToContent = WinJS.Binding.converter(function (imageId) { //return "http://somesite.com/someimage.jpg"; //this will work

    //get the image by calling a WCF Service WinJS.xhr({ type: "GET", url: "http://localhost:2738/someservice.svc/getcontent?imageId=" + imageId, responseType: "blob" }).done(processResult, processError); //will not work because the converter does not return the value of the success-callback method }); function processResult(response) { var imagestream = URL.createObjectURL(response.response); return imagestream; }


    Is it possible to return the value of an async method as the result of a bindingconverter?


    • Edited by D. Tiemstra Thursday, March 28, 2013 3:48 PM
    Thursday, March 28, 2013 3:47 PM
  • Hi D,

    No, the converter takes a function not a promise:

    http://msdn.microsoft.com/en-us/library/windows/apps/br229809.aspx

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, March 28, 2013 4:00 PM
    Moderator