locked
how i can eject variable samplesGroups of WinJS.xhr

    Question

  • any body can help me???

    how i can eject variable samplesGroups in WinJS.xhr function...below is sample code.

    function generateSampleData() {
            WinJS.xhr({ url: 'http://sso.okelocal.net/win8.php' }).done(function (res)
            {
                var samplesGroups = [];
                var data = JSON.parse(res.responseText);
                for (var i = 0, itemsLength = data.length; i < itemsLength; i++) {
                    samplesGroups.push({
                        key  : 'group'+i,
                        title: data[i].title,
                        subtitle: data[i].subtitle,
                        backgroundImage: data[i].backgroundImage,
                        description: data[i].description
                    });
                }
                //callback(samplesGroups)
                //return samplesGroups;

            });
            console.log(samplesGroups);
            // Each of these sample groups must have a unique key to be displayed
            // separately.
           var sampleItems;= samplesGroups;
           
            return sampleItems;
    }

    Tuesday, October 23, 2012 10:16 AM

Answers

  • Hi iwan,

    the javascript function you supplied for the WinJS.xhr().done() is a callback which is automatically invoked when the web request successfully completed. We should not use it to return data because asynchronous programming pattern doesn't quite rely on return value to pass data. One possible approach for you to transfer data is defining some global data variable through "WinJS.Namespace.define" , and then use the global variable in the certain functions which need them. Here is a simple example which use global variable to hold list of RSS feed items after the xhr call completed.

    (function () {
        "use strict";
    
        var feedItems = [];
    
    
        function loadBlogFeedsFromWebAsXML() {
    
            var feedUrl = "http://blogs.msdn.com/b/windowsstore/rss.aspx";
    
            WinJS.xhr({url:feedUrl}).then(
            function (result) {
    
                var xmlDoc = new Windows.Data.Xml.Dom.XmlDocument();
                xmlDoc.loadXml(result.responseText);
    
                var nodes = xmlDoc.selectNodes("//item");
    
                var items = new Array();
    
                nodes.forEach(function (val, idx, travObj) {
    
                    var title = val.selectSingleNode("title").innerText;
                    var description = val.selectSingleNode("description").innerText;
                    var link = val.selectSingleNode("link").innerText;
    
                    var newItem = {
                        title: title,
                        summary: description,
                        uri:link
                    };
    
                    items.push(newItem);
    
                    
                }
                );
    
                DataUtils.feedItems = items;
    
    
                // Call other function which can use the DataUtils.feedItems to bind UI elements
    
    		            
            },
            function (err) {
            }
            );
    
        }
    
      
    
    
    
        WinJS.Namespace.define(
            "DataUtils", {
                feedItems: feedItems
            }
            );
    })();


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. Putting communities in your palms. Launch the browser on your phone now, type aka.ms/msforums and get connected!

    Wednesday, October 24, 2012 7:40 AM
    Moderator

All replies

  • Hi,

    I think the code is no problem.

    Please check the below code:

     var sampleItems;= samplesGroups;


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com
    Microsoft One Code Framework

    Wednesday, October 24, 2012 3:45 AM
  • Hi iwan,

    the javascript function you supplied for the WinJS.xhr().done() is a callback which is automatically invoked when the web request successfully completed. We should not use it to return data because asynchronous programming pattern doesn't quite rely on return value to pass data. One possible approach for you to transfer data is defining some global data variable through "WinJS.Namespace.define" , and then use the global variable in the certain functions which need them. Here is a simple example which use global variable to hold list of RSS feed items after the xhr call completed.

    (function () {
        "use strict";
    
        var feedItems = [];
    
    
        function loadBlogFeedsFromWebAsXML() {
    
            var feedUrl = "http://blogs.msdn.com/b/windowsstore/rss.aspx";
    
            WinJS.xhr({url:feedUrl}).then(
            function (result) {
    
                var xmlDoc = new Windows.Data.Xml.Dom.XmlDocument();
                xmlDoc.loadXml(result.responseText);
    
                var nodes = xmlDoc.selectNodes("//item");
    
                var items = new Array();
    
                nodes.forEach(function (val, idx, travObj) {
    
                    var title = val.selectSingleNode("title").innerText;
                    var description = val.selectSingleNode("description").innerText;
                    var link = val.selectSingleNode("link").innerText;
    
                    var newItem = {
                        title: title,
                        summary: description,
                        uri:link
                    };
    
                    items.push(newItem);
    
                    
                }
                );
    
                DataUtils.feedItems = items;
    
    
                // Call other function which can use the DataUtils.feedItems to bind UI elements
    
    		            
            },
            function (err) {
            }
            );
    
        }
    
      
    
    
    
        WinJS.Namespace.define(
            "DataUtils", {
                feedItems: feedItems
            }
            );
    })();


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. Putting communities in your palms. Launch the browser on your phone now, type aka.ms/msforums and get connected!

    Wednesday, October 24, 2012 7:40 AM
    Moderator
  • Hi,

    Yes, as steven said. You must do anything you want in winjs.done() or winjs.then(), but after the function. Because winjs.xhr is a asynchronous function.

    Thank you for steven's explanation!


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com
    Microsoft One Code Framework

    Wednesday, October 24, 2012 10:16 AM