Riot JS卸载页面中的所有标签,然后只挂载一个标签不起作用

时间:2015-06-14 06:15:49

标签: riot.js

我正在使用Riot JS,在我的index.html中,我有3个自定义标签 - 标题,登录面板和候选人面板。在我的主app.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)
    })
})

3 个答案:

答案 0 :(得分:3)

回答riot.js v2.1.0:

功能

riot.unmount(...)
据我所知,

不可用。但是,您可以卸载已保存的标签。

mytag.unmount(true) 

Source

诀窍是要记住挂载的标签以便以后能够卸载它们:

var viewTag = riot.mount(document.getElementById('viewTag'))
viewTag.unmount(true)

您可以将所有这些视图标记存储在对象中并循环它们以卸载所有视图标记并仅挂载活动标记。

Source

答案 1 :(得分:1)

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

答案 2 :(得分:1)

我认为您正在寻找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);
相关问题