Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript amplify.js-amplify.store不存储数据_Javascript_Mvvm_Amplifyjs - Fatal编程技术网

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

Javascript amplify.js-amplify.store不存储数据,javascript,mvvm,amplifyjs,Javascript,Mvvm,Amplifyjs,我将amplify.js与Knockout.js一起使用,我希望在本地存储数据。我尝试使用以下代码: 但这对我不起作用 我的视图模型 define(['services/datacontext'], function (dataContext) { var store = amplify.store("offlineData"); // Why is agency undefined after retrieving from the store?!?!?! var agen

我将amplify.js与Knockout.js一起使用,我希望在本地存储数据。我尝试使用以下代码: 但这对我不起作用

我的视图模型

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存储包含这个数据,但它是未定义的

有人知道如何使用amplify吗

amplify.store("offlineData", vm.agency);
代理是一个函数,因此需要调用它来获取其值

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

是的,你可能是对的。我更喜欢使用Knockout实用程序
ko.util.unwrapObservable(vm.agency)
,或者“更新的”
ko.unwrap(vm.agency)
,在IMO中读起来更好-使它更明确-而且如果您曾经将事情更改为不可观察的(虽然不太可能),那么它不会破坏代码。