我有两个不同的userTypes'读者'和'发布者'。此信息存储在Meteor.users文档中,Meteor.users.userType: 'publisher'
或者可以Meteor.users.userType:'reader'
如何根据用户类型向appBody动态添加元素?
数据上下文和菜单项之类的内容对于userTypes当然是不同的
我已经引用了https://github.com/EventedMind/iron-dynamic-template
HTML:
<head>
<title>Site</title>
</head>
<template name="appBody"> //this is layoutTemplate
<div class="navbar navbar-fixed-top navbar-custom">
<div class = "container">
<ul class="nav navbar-nav navbar-right">
<li><a href="#">menu title A</a></li>
<li><a href="#">menu title B</a></li>
***DYNAMICALLY ADD LIST ELEMENT DEPENDING ON WHICH 'userType' IS LOGGED IN HERE***
</ul>
</div>
</div>
{{> UI.dynamic template=currentUser.userType }}
</template>
<template name="reader">
unique layout
{{> yield}}
</template>
<template name="publisher">
unique layout
{{> yield}}
</template>
router.js
Router.configure({
layoutTemplate: 'appBody',
loadingTemplate: 'appLoading'
});
我以某种方式将应用程序破坏为已登录状态,而不是已登录状态。当用户确实登录时,根据他们的userType,我将呈现“阅读器”模板或“发布者”模板。可以这么说,这两个现在都充当每个用户的布局模板。然后,我可以使用iron:router将模板屈服于这些模板。
Router.configure({
layoutTemplate: 'parent',
loadingTemplate: 'appLoading'
});
<template name="parent">
{{#if currentUser}}
{{> UI.dynamic template=currentUser.userType }} //this returns 'reader' or 'publisher'
{{else}}
{{> notLoggedIn}}
{{/if}}
</template>
<template name="notLoggedIn">
//unique styling for non logged in users such as join and log in.
{{> yield}}
</template>
<template name="reader">
//unique styling and custom reader navigation menu
{{> yield}}
</template>
<template name="publisher">
//unique styling and custom publisher navigation menu
{{> yield}}
</template>
现在,我可以将任何模板从Router.route渲染到yield区域:))
Router.route('/path', function () {
this.render('templateName');
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句