Javascript Ember.js-一视图多布局(已登录/未登录)
在我当前的Ember项目中,我有一个适当的身份验证系统。某些页面只能由经过身份验证的用户查看。检查用户是否经过身份验证并控制对页面的访问非常简单,因此没有问题 问题是,我有一些页面可以被经过身份验证和未经身份验证的用户查看。经过身份验证的用户看到的导航(顶部和侧栏)与未经身份验证的用户不同(更多导航选项、设置等)。我想根据用户是否经过身份验证来更改使用的布局。问题是我似乎只能为一个视图设置一个布局 一般代码如下所示: 布局:Javascript Ember.js-一视图多布局(已登录/未登录),javascript,ember.js,Javascript,Ember.js,在我当前的Ember项目中,我有一个适当的身份验证系统。某些页面只能由经过身份验证的用户查看。检查用户是否经过身份验证并控制对页面的访问非常简单,因此没有问题 问题是,我有一些页面可以被经过身份验证和未经身份验证的用户查看。经过身份验证的用户看到的导航(顶部和侧栏)与未经身份验证的用户不同(更多导航选项、设置等)。我想根据用户是否经过身份验证来更改使用的布局。问题是我似乎只能为一个视图设置一个布局 一般代码如下所示: 布局: <script type="text/x-handlebars"
<script type="text/x-handlebars" data-template-name="authenticated_layout">
//authenticated layout mark up
{{yield}}
</script>
<script type="text/x-handlebars" data-template-name="not_authenticated_layout">
//not_authenticated layout mark up
{{yield}}
</script>
我已经为每个布局创建了一个不同的视图,并根据用户是否经过身份验证来使用该视图呈现模板。我看到的问题是,ArticlesView
不仅仅是设置布局,我真的不希望有两个单独的视图,它们只在layoutName
属性上有所不同
任何建议都将不胜感激。我认为您的layoutName中的计算属性可以工作 伪代码:
App.ArticleView = Ember.View.extend({
templateName: "article",
layoutName: function() {
// you can use your own logic to know if the user is authenticated
// but don't forget to add in the property(dependenKey), if needed
return App.get('currentUser') ? 'authenticated_layout' : 'not_authenticated_layout';
}.property('App.currentUser')
})
啊,是的,这很有效。我不知道计算属性可以在那里使用,但它是有意义的。谢谢
App.ArticleView = Ember.View.extend({
templateName: "article",
layoutName: //want this to be based on authentication state
//other view code
})
App.ArticleView = Ember.View.extend({
templateName: "article",
layoutName: function() {
// you can use your own logic to know if the user is authenticated
// but don't forget to add in the property(dependenKey), if needed
return App.get('currentUser') ? 'authenticated_layout' : 'not_authenticated_layout';
}.property('App.currentUser')
})