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


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


JS-检查(无法正常工作)

  if (localStorage.getItem('GlobalNavigation') == null) {
        $_global_Mavention_GlobalNavigation_Some_Other_Stuff();
    } else {
        var retrievedData = JSON.parse(localStorage.getItem('GlobalNavigation'));
        ko.mapping.fromJS(GlobalNavigation, {}, self);
        $_global_Set_GlobalNavigation_Node_Active();
    }


的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>


JS

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')));
    }
}

关于javascript - 在本地存储中设置/获取ViewModel(如果存在)- knockout -javascript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25307143/

10-11 20:01
查看更多