我对如何处理命名空间感到有点困惑:
在我的母版页中定义了我网站上所有页面的布局,我有以下内容:
<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);
}
});
答案 0 :(得分:0)
这不起作用:
var Layout.sidebar = localStorage.getItem('Sidebar');
简单地做
Layout.sidebar = localStorage.getItem('Sidebar');
这会将sidebar
属性添加到您之前创建的Layout
对象中。