locked
Get Data (an Array in my case) from default js into another js File RRS feed

  • Question

  • Hello everyone

    I recently started coding javascript/html on Visual Studio and stumbled over a few problems.My major problem is the asynchronous behaviour, being young and not too experienced i have a hard time searching through everything and am now after several days confused.

    I just want to get some JSON data before i start the whole thing, so that the JSON data can be used for title etc of my split App.

    WinJS.xhr({ url: url, responseType: "text" })
                .done(function complete(result) {
                    var responseText = eval(result.responseText);

                    //testDiv.innerHTML = responseText;
                    for (var i = 0; i < responseText.length; i++) {
                        // testDiv.innerHTML += responseText[i]["name"] + "<br />";
                        dataArray[i]["title"]=responseText[i]["name"];
                    }
                   
                    WinJS.Promise.join(dataArray).then(function myFunction() {
                        var list = new WinJS.Binding.List();
                        var groupedItems = list.createGrouped(
                            function groupKeySelector(item) { return item.group.key; },
                            function groupDataSelector(item) { return item.group; }
                        );

    now while this is working my problem is that the .js file that is receiving the data is called to early and there is no data available. I tried to solve this by adding a promis.timeout(1000) to the oder .js naturally this doesnt work becaus now my app is just empty because the whole data thing waits 1000 and the ui builder builds what is there - nothing.

    Now my main problem:

    How can i use:

    WinJS.xhr({ url: url, responseType: "text" })
                .done(function complete(result) {
                    var responseText = eval(result.responseText);

                    //testDiv.innerHTML = responseText;
                    for (var i = 0; i < responseText.length; i++) {
                        // testDiv.innerHTML += responseText[i]["name"] + "<br />";
                        dataArray[i]["title"]=responseText[i]["name"];
                    }

    this data i get here in my other .js file namely the data.js (this code i would place in the onlaunch section of the default.js) i tried with namespace but i figured this is only for the actual UI part, correct me if im wrong.

    Thanks in advance for all answers

    • Moved by Mark Liu-lxf Monday, September 24, 2012 3:25 AM (From:Visual Basic Express Edition)
    Friday, September 21, 2012 10:04 AM

Answers

  • Hi Frog,

    Call the function to get the data in your app.onactivated function after you call WinJS.UI.processAll() something like this:

     app.onactivated = function (args) {
            if (args.detail.kind === activation.ActivationKind.launch) {
                if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                    // TODO: This application has been newly launched. Initialize
                    // your application here.
                } else {
                    // TODO: This application has been reactivated from suspension.
                    // Restore application state here.
                }
                args.setPromise(WinJS.UI.processAll().then(
    //call your function here!!!
     ));
            }
        };


    Jeff Sanders (MSFT)

    Tuesday, September 25, 2012 12:49 PM
    Moderator

All replies

  • Hi Frog,

    Call the function to get the data in your app.onactivated function after you call WinJS.UI.processAll() something like this:

     app.onactivated = function (args) {
            if (args.detail.kind === activation.ActivationKind.launch) {
                if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                    // TODO: This application has been newly launched. Initialize
                    // your application here.
                } else {
                    // TODO: This application has been reactivated from suspension.
                    // Restore application state here.
                }
                args.setPromise(WinJS.UI.processAll().then(
    //call your function here!!!
     ));
            }
        };


    Jeff Sanders (MSFT)

    Tuesday, September 25, 2012 12:49 PM
    Moderator
  • Thank you very much, ill try it out the next time i get my hands on it.

    Friday, September 28, 2012 7:14 AM