Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.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
在localstorage中设置/获取viewmodel(如果存在)-敲除-javascript_Javascript_Knockout.js_Local Storage_Viewmodel_Csom - Fatal编程技术网

在localstorage中设置/获取viewmodel(如果存在)-敲除-javascript

在localstorage中设置/获取viewmodel(如果存在)-敲除-javascript,javascript,knockout.js,local-storage,viewmodel,csom,Javascript,Knockout.js,Local Storage,Viewmodel,Csom,我正在用CSOM检索我的全局导航。但执行起来需要一些时间。我想要的是在第一次加载页面时将ViewModel存储在localstorage中 我在检索ViewModel时没有任何问题,但我需要一些 帮助检查viewmodel是否存在/获取视图模型/设置 视图模型正确 检查viewmodel是否已更改的最佳方法是什么?(示例代码) JS-检查(工作不正常) HTML <div id="customGlobalNavigation"> <script src="../../_

我正在用CSOM检索我的全局导航。但执行起来需要一些时间。我想要的是在第一次加载页面时将ViewModel存储在localstorage中

  • 我在检索ViewModel时没有任何问题,但我需要一些 帮助检查viewmodel是否存在/获取视图模型/设置 视图模型正确
  • 检查viewmodel是否已更改的最佳方法是什么?(示例代码)
  • JS-检查(工作不正常)

    HTML

    <div id="customGlobalNavigation">
        <script src="../../_layouts/15/SuperSite/js/Knockout.js"></script>
        <script src="../../_layouts/15/SuperSite/js/globalNavigation.js"></script>
        <script>
            Mavention.GlobalNavigation.init('Managed Metadata Service', '88888888-5ce7-47cc-a696-c67f8c99943a');
        </script>
        <div class="noindex ms-core-listMenu-horizontalBox">
            <ul class="root ms-core-listMenu-root static" data-bind="foreach: globalMenuItems">
                <li class="static">
                    <a class="static menu-item ms-core-listMenu-item ms-displayInline ms-navedit-linkNode" data-bind="attr: { href: url }">
                        <span class="additional-background ms-navedit-flyoutArrow">
                            <span class="menu-item-text" data-bind="text: title"></span>
                        </span>
                    </a>
                </li>
            </ul>
        </div>
        </div>
    

    您可以按如下方式设置和获取本地存储:

    private saveGlobalNavigationToLocalStorage() {
        localStorage.setItem('GlobalNavigation', JSON.stringify(this.YOUR_OBJECT));
    }
    
    public getGlobalNavigation() {
        // check if it exists already
        if (!localStorage.getItem('GlobalNavigation')) {
            // If NOT - Perform your get & assign value to this.YOUR_OBJECT
    
            // Then call the save method to add to local storage
            this.saveGlobalNavigationToLocalStorage();                           
        } else {
            // Otherwise return object from local storage
            this.YOUR_OBJECT(JSON.parse(localStorage.getItem('GlobalNavigation')));
        }
    }
    
    Mavention.GlobalNavigation.loadNavigationInternal = function () {
    var context = SP.ClientContext.get_current();
    var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
    var termStore = taxonomySession.get_termStores().getByName(this.termStoreName);
    var termSet = termStore.getTermSet(this.termSetId);
    var terms = termSet.getAllTerms();
    context.load(terms);
    context.executeQueryAsync(Function.createDelegate(this, function (sender, args) {
        var termsEnumerator = terms.getEnumerator();
        var menuItems = new Array();
    
        while (termsEnumerator.moveNext()) {
            var currentTerm = termsEnumerator.get_current();
            Mavention.GlobalNavigation.viewModel.globalMenuItems.push(new Mavention.GlobalNavigation.MenuItem(currentTerm.get_name(), currentTerm.get_localCustomProperties()['_Sys_Nav_SimpleLinkUrl']));
        }
    
        ko.applyBindings(Mavention.GlobalNavigation.viewModel);
        localStorage.setItem('GlobalNavigation', Mavention.GlobalNavigation.viewModel);
        SP.UI.Notify.removeNotification(this.nid);
        $_global_Set_GlobalNavigation_Node_Active();
        $("#customGlobalNavigation > div").show();
    }), Function.createDelegate(this, function (sender, args) {
        alert('The following error has occured while loading global navigation: ' + args.get_message());
    }));
    };
    
    private saveGlobalNavigationToLocalStorage() {
        localStorage.setItem('GlobalNavigation', JSON.stringify(this.YOUR_OBJECT));
    }
    
    public getGlobalNavigation() {
        // check if it exists already
        if (!localStorage.getItem('GlobalNavigation')) {
            // If NOT - Perform your get & assign value to this.YOUR_OBJECT
    
            // Then call the save method to add to local storage
            this.saveGlobalNavigationToLocalStorage();                           
        } else {
            // Otherwise return object from local storage
            this.YOUR_OBJECT(JSON.parse(localStorage.getItem('GlobalNavigation')));
        }
    }