locked
Saving ListView data

    Question

  • Hi,

    Newbie question but I have a ListView that contains user-entered items that I want to persist so that they are saved and available the next time the app is launched. What is the best way to do this? I have a global variable called DataModel that contains a list of items:

    var activitiesList = new WinJS.Binding.List(activitiesArray);
    
    var publicMembers =
    {
        itemList: activitiesList
    };
    
    WinJS.Namespace.define("DataSource", publicMembers);

    where activitiesList is an array that contains subarrays.

    Thanks! S


    • Edited by sarrowsmith Monday, October 22, 2012 2:19 AM
    Monday, October 22, 2012 2:18 AM

Answers

  • Try this to save:

    var jsonText = JSON.stringify(activitiesArray)
    
       var localFolder = Windows.Storage.ApplicationData.current.localFolder;
                        localFolder.createFileAsync("myfile.data", Windows.Storage.CreationCollisionOption.replaceExisting)
                            .then(function (file) {
                                return Windows.Storage.FileIO.writeTextAsync(file, jsonText);
                            });
    

    Then this to load:

    var localFolder = Windows.Storage.ApplicationData.current.localFolder;
    
    localFolder.getFileAsync("myfile.data")
                    .then(function (file) {
                        return Windows.Storage.FileIO.readTextAsync(file);
                    }).done(function (text) {
                        activitiesArray = JSON.parse(text);
                    });
            });


    Dave Paquette @Dave_Paquette www.davepaquette.com

    • Proposed as answer by Can BilginMVP Monday, October 22, 2012 12:37 PM
    • Marked as answer by sarrowsmith Tuesday, October 23, 2012 2:15 PM
    Monday, October 22, 2012 2:37 AM
  • Just add to Dave's suggestion:

    If the data is not quite large, you can directly use the Local application settings to store them. e.g.

    #Quickstart: Local application data (Windows Store apps using JavaScript and HTML)
    http://msdn.microsoft.com/en-us/library/windows/apps/hh465118.aspx

    You can use JSON.stringify to convert your JSON data into string and store into local settings. And later retrieve them back via JSON.parse. Here is a simple function to demonstrate it.

    (function () {
        "Use Strict";
    
        var localSettings = Windows.Storage.ApplicationData.current.localSettings;
    
        function loadLocalSettingData(key) {
            var txt = localSettings.values[key];
    
            console.log("loadLocalSettingData('" + key + "')");
            var data = JSON.parse(txt);
    
            return data;
        }
    
        function saveLocalSettingData(key, data) {
    
            var txt = JSON.stringify(data);
    
            localSettings.values[key] = txt;
    
            console.log("saveLocalSettingData('" + key + "')");
        }
    
        
        WinJS.Namespace.define("LocalAppStorage", {
            loadLocalSetting: loadLocalSettingData,
            saveLocalSetting: saveLocalSettingData
        });
    
    })();




    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!

    • Marked as answer by sarrowsmith Tuesday, October 23, 2012 2:15 PM
    Tuesday, October 23, 2012 7:44 AM
    Moderator

All replies

  • Try this to save:

    var jsonText = JSON.stringify(activitiesArray)
    
       var localFolder = Windows.Storage.ApplicationData.current.localFolder;
                        localFolder.createFileAsync("myfile.data", Windows.Storage.CreationCollisionOption.replaceExisting)
                            .then(function (file) {
                                return Windows.Storage.FileIO.writeTextAsync(file, jsonText);
                            });
    

    Then this to load:

    var localFolder = Windows.Storage.ApplicationData.current.localFolder;
    
    localFolder.getFileAsync("myfile.data")
                    .then(function (file) {
                        return Windows.Storage.FileIO.readTextAsync(file);
                    }).done(function (text) {
                        activitiesArray = JSON.parse(text);
                    });
            });


    Dave Paquette @Dave_Paquette www.davepaquette.com

    • Proposed as answer by Can BilginMVP Monday, October 22, 2012 12:37 PM
    • Marked as answer by sarrowsmith Tuesday, October 23, 2012 2:15 PM
    Monday, October 22, 2012 2:37 AM
  • Just add to Dave's suggestion:

    If the data is not quite large, you can directly use the Local application settings to store them. e.g.

    #Quickstart: Local application data (Windows Store apps using JavaScript and HTML)
    http://msdn.microsoft.com/en-us/library/windows/apps/hh465118.aspx

    You can use JSON.stringify to convert your JSON data into string and store into local settings. And later retrieve them back via JSON.parse. Here is a simple function to demonstrate it.

    (function () {
        "Use Strict";
    
        var localSettings = Windows.Storage.ApplicationData.current.localSettings;
    
        function loadLocalSettingData(key) {
            var txt = localSettings.values[key];
    
            console.log("loadLocalSettingData('" + key + "')");
            var data = JSON.parse(txt);
    
            return data;
        }
    
        function saveLocalSettingData(key, data) {
    
            var txt = JSON.stringify(data);
    
            localSettings.values[key] = txt;
    
            console.log("saveLocalSettingData('" + key + "')");
        }
    
        
        WinJS.Namespace.define("LocalAppStorage", {
            loadLocalSetting: loadLocalSettingData,
            saveLocalSetting: saveLocalSettingData
        });
    
    })();




    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!

    • Marked as answer by sarrowsmith Tuesday, October 23, 2012 2:15 PM
    Tuesday, October 23, 2012 7:44 AM
    Moderator
  • Thanks both for the feedback - this works great!

    Tuesday, October 23, 2012 2:15 PM