How can I implement MVVM with offline storage and Knockout.js?

后端 未结 4 1001
旧巷少年郎
旧巷少年郎 2021-02-09 04:22

I can implement Mvvm with Knockout.js. But I want to use it with cross browser(FF and Chrome) supported Html 5 offline storage.

I want to bind html objects to offline

4条回答
  •  离开以前
    2021-02-09 04:57

    I worked out a solution bases on the subscribe feature of KnockoutJS. It takes a model and persist all the observable properties.

    ko.persistChanges = function (vm, prefix) {
    
        if (prefix === undefined) {
            prefix = '';
        }
    
        for (var n in vm) {
    
            var observable = vm[n];
            var key = prefix + n;
    
            if (ko.isObservable(observable) && !ko.isComputed(observable)) {
    
                //track change of observable
                ko.trackChange(observable, key);
    
                //force load
                observable();
            }
        }
    };
    

    Check http://keestalkstech.com/2014/02/automatic-knockout-model-persistence-offline-with-amplify/ for code and JSFiddle example.

提交回复
热议问题