amplify.js - amplify.store不存储数据

时间:2013-08-08 11:47:25

标签: javascript mvvm amplifyjs

我正在使用带有Knockout.js的amplify.js,我想在本地存储数据。我尝试使用此代码:amplify guide 但它不适合我。

我的观点模型

define(['services/datacontext'], function (dataContext) {

    var store = amplify.store("offlineData"); // Why is agency undefined after retrieving from the store?!?!?!

    var agency = ko.observableArray([]);
    var initialized = false;

    var save = function (agency) {
        return dataContext.saveChanges(agency);
    };

    var vm = { // This is my view model, my functions are bound to it. 
        //These are wired up to my agency view
        activate: activate,
        agency: agency,
        title: 'agency',
        refresh: refresh, // call refresh function which calls get Agencies
        save: save
    };
    return vm;

    function activate() {
        if (initialized) {
            return;
        }

        initialized = true;

        if (initialized == true) {
            amplify.store("offlineData", vm.agency);
        }

        return refresh();

    }

    function refresh() {
        return dataContext.getAgency(agency);
    }
});

刷新后检索数据,我将此数据保存到本地存储。所以,当我再次请求此页面时。我希望var store包含这些数据,但它是未定义的。

有谁知道如何使用放大?

1 个答案:

答案 0 :(得分:1)

amplify.store("offlineData", vm.agency);

vm.agency是一个函数,因此您需要调用它来获取其值

amplify.store("offlineData", vm.agency());