Riot JS 卸载页面中的所有标签,然后仅安装一个标签不起作用
Riot JS unmount all tags in a page and then mount only one tag is not working
我正在使用 Riot JS,在我的 index.html 中,我有 3 个自定义标签 - header、login-panel 和 candidates-panel 在我体内。在我的mainapp.js中,在$(document).ready的回调函数中,执行了当前路由,同时注册了一个路由变更处理函数。在我的 switchView 中,我卸载了所有自定义标签,然后尝试仅安装与正在切换的当前视图相关的标签。这是我的代码。如果我卸载,那么页面上不会显示任何内容
index.html
<body>
<header label="Hire Zen" icon="img/user-8-32.png"></header>
<login-panel class="viewTag" id="loginView"></login-panel>
<candidates-panel id="candidatesView" class="viewTag"></candidates-panel>
<script src="js/bundle.js"></script>
</body>
app.js
function switchView(view) {
if(!view || view === '') {
view = 'login'
}
//unmount all other panels and mount only the panel that is required
//TODO: unmount all view panels and mounting only required panel is not working
//riot.unmount('.viewTag')
riot.mount(view+'-panel')
$('.viewTag').hide()
$(view+'-panel').show()
}
$(document).ready(function () {
RiotControl.addStore(new AuthStore())
RiotControl.addStore(new CandidatesStore())
riot.mount('header')
//register route change handler
riot.route(function (collection, id, action) {
switchView(collection)
})
riot.route.exec(function (collection, id, action) {
switchView(collection)
})
})
riot.js v2.1.0 的回答:
函数
riot.unmount(...)
据我所知不可用。但是,您可以卸载已保存的标签。
mytag.unmount(true)
诀窍是记住已挂载的标签,以便稍后卸载它们:
var viewTag = riot.mount(document.getElementById('viewTag'))
viewTag.unmount(true)
您可以将所有这些视图标签存储在一个对象中并循环它们以卸载所有视图标签并仅安装活动标签。
2.3.18 的答案
根据之前的回答和this tutorial我创建了以下概念:
app.currentPage = null;
var goTo = function(page){
if (app.currentPage) {
app.currentPage.unmount(true); //unmount and keep parent tag
}
app.currentPage = riot.mount(page)[0]; //remember current page
};
riot.route(function() {
console.info("this page is not defined");
//do nothing (alternatively go to 404 page or home)
});
riot.route('/inventory', function(){
goTo('inventory');
});
riot.route('/options', function() {
goTo('options');
});
我想你正在寻找 riot.util.tags.unmountAll(tags)
如何实现目标?
index.html
var tags = [];
some.tag.html
var some = this;
tags.push(some);
unmountAllTags.js
riot.util.tags.unmountAll(tags);
我正在使用 Riot JS,在我的 index.html 中,我有 3 个自定义标签 - header、login-panel 和 candidates-panel 在我体内。在我的mainapp.js中,在$(document).ready的回调函数中,执行了当前路由,同时注册了一个路由变更处理函数。在我的 switchView 中,我卸载了所有自定义标签,然后尝试仅安装与正在切换的当前视图相关的标签。这是我的代码。如果我卸载,那么页面上不会显示任何内容
index.html
<body>
<header label="Hire Zen" icon="img/user-8-32.png"></header>
<login-panel class="viewTag" id="loginView"></login-panel>
<candidates-panel id="candidatesView" class="viewTag"></candidates-panel>
<script src="js/bundle.js"></script>
</body>
app.js
function switchView(view) {
if(!view || view === '') {
view = 'login'
}
//unmount all other panels and mount only the panel that is required
//TODO: unmount all view panels and mounting only required panel is not working
//riot.unmount('.viewTag')
riot.mount(view+'-panel')
$('.viewTag').hide()
$(view+'-panel').show()
}
$(document).ready(function () {
RiotControl.addStore(new AuthStore())
RiotControl.addStore(new CandidatesStore())
riot.mount('header')
//register route change handler
riot.route(function (collection, id, action) {
switchView(collection)
})
riot.route.exec(function (collection, id, action) {
switchView(collection)
})
})
riot.js v2.1.0 的回答:
函数
riot.unmount(...)
据我所知不可用。但是,您可以卸载已保存的标签。
mytag.unmount(true)
诀窍是记住已挂载的标签,以便稍后卸载它们:
var viewTag = riot.mount(document.getElementById('viewTag'))
viewTag.unmount(true)
您可以将所有这些视图标签存储在一个对象中并循环它们以卸载所有视图标签并仅安装活动标签。
2.3.18 的答案
根据之前的回答和this tutorial我创建了以下概念:
app.currentPage = null;
var goTo = function(page){
if (app.currentPage) {
app.currentPage.unmount(true); //unmount and keep parent tag
}
app.currentPage = riot.mount(page)[0]; //remember current page
};
riot.route(function() {
console.info("this page is not defined");
//do nothing (alternatively go to 404 page or home)
});
riot.route('/inventory', function(){
goTo('inventory');
});
riot.route('/options', function() {
goTo('options');
});
我想你正在寻找 riot.util.tags.unmountAll(tags)
如何实现目标?
index.html
var tags = [];
some.tag.html
var some = this;
tags.push(some);
unmountAllTags.js
riot.util.tags.unmountAll(tags);