0

我对如何处理命名空间有点困惑:

在我定义网站上所有页面布局的母版页中,我有以下内容:

<script type="text/javascript">
</script>
@Scripts.Render("~/bundles/theme")
@Scripts.Render("~/bundles/common")
@Scripts.Render("~/bundles/layout")
@Scripts.Render("~/bundles/user")

这是布局包中唯一的脚本:

$(document).ready(function () {
    "use strict";
    addAjaxControls();
    addAccessControls();
    addBodyControls();
    addContentControls();
    addThemeControls();

    if (localStorage.getItem('Sidebar')) {
        var sidebar = localStorage.getItem('Sidebar');
        setSidebar(sidebar);
    }
    if (localStorage.getItem('Theme')) {
        var theme = localStorage.getItem('Theme')
        setTheme(theme);
    }
});

我想保持明确的定义,因此添加以下内容是否合理且是个好主意:

<script type="text/javascript">
    var Layout = Layout || {};
</script>
@Scripts.Render("~/bundles/theme")
@Scripts.Render("~/bundles/common")
@Scripts.Render("~/bundles/layout")
@Scripts.Render("~/bundles/user")

然后在我的 layout.js 中:

$(document).ready(function () {
    "use strict";
    addAjaxControls();
    addAccessControls();
    addBodyControls();
    addContentControls();
    addThemeControls();

    if (localStorage.getItem('Sidebar')) {
        var Layout.sidebar = localStorage.getItem('Sidebar');
        setSidebar(Layout.sidebar);
    }
    if (localStorage.getItem('Theme')) {
        var Layout.theme = localStorage.getItem('Theme')
        setTheme(Layout.theme);
    }
});
4

1 回答 1

0

这是行不通的:

var Layout.sidebar = localStorage.getItem('Sidebar');

简单地做

Layout.sidebar = localStorage.getItem('Sidebar');

这会将sidebar属性添加到Layout您之前创建的对象中。

于 2012-10-03T16:35:54.250 回答