locked
problem with xhr

    Question

  • hi everyone, i'm here because i develop a metro apps and i have a small problem.

    In a html page i call 2 different .js page, the first to get the data from a Webservice (getData.js), and the second to desplay the data(displayData.js).

    For get the data i used WinJS.xhr and after var items = JSON.parse(xhr.responseText);

    my probleme was the code into the displayData.js was execute before the WinJS.xhr and after var items = JSON.parse(xhr.responseText); was finished so that don't work.

    I wan't to know how to execute the code in the displayData.js before getting all the data was finished.

    Thank You ( sorry for my english, i'm french :) )


    Sunday, April 8, 2012 5:54 PM

Answers

  • try this:

    data.js

    //in data.js
    
    (function(){
        "use strict";
        function getData(callback){
            WinJS.xhr({ url: 'url' }).done(function complete(result){
                var data = JSON.parse(result.responseText);
                callback(data)
            })
        }
    
        WinJS.Namespace.define('data',{
            getData: getData
        })
    })();
    
    
    //in displayData.js
    
    (function(){
    
    
        data.getData(function(data){
            //display data;
            WinJS.Binding.processAll('your template', data)
        })
    })()

    I can only think of this
    Monday, April 9, 2012 1:48 AM

All replies

  • try this:

    data.js

    //in data.js
    
    (function(){
        "use strict";
        function getData(callback){
            WinJS.xhr({ url: 'url' }).done(function complete(result){
                var data = JSON.parse(result.responseText);
                callback(data)
            })
        }
    
        WinJS.Namespace.define('data',{
            getData: getData
        })
    })();
    
    
    //in displayData.js
    
    (function(){
    
    
        data.getData(function(data){
            //display data;
            WinJS.Binding.processAll('your template', data)
        })
    })()

    I can only think of this
    Monday, April 9, 2012 1:48 AM
  • Thats work perfectly, thank you :)
    Monday, April 9, 2012 8:07 AM