2015-08-28 24 views
2

在我的@yield('scripts')部分有一个javascript调用,如果用户是管理员,我只显示一个按钮。Laravel如何在JavaScript文件中访问Auth。

我想将JavaScript放在我的库中,因为它被多次使用。然而,当我尝试包括在库本节叶片/ PHP不工作myApp.js

JavaScript部分

BootstrapDialog.show({ 
    title: 'Age Not Met', 
message: 'The attendee does not meet the age reguirement for this program.', 
    type: BootstrapDialog.TYPE_WARNING, 
buttons: [ 
      @if (Auth::user()->hasRole('admin')||Auth::user()->hasRole('programmer')) 
      { 
       label: 'OVERIDE', 
      cssClass: 'btn-danger', 
      action: function(dialogItself){ 
        checkbox.prop('checked', true); 
//...rest irrelevant 

我想在我的布局中使用一个变量来更换刀片/ PHP的通话但变量是空白的。此外,我意识到我不能只用一个if(isAdmin)替换刀片,因为它在一个函数参数中...我该如何让按钮仅出现在管理员中?

布局

<script> 
var isAdmin = {{ (Auth::user()->hasRole('admin')||Auth::user()->hasRole('programmer')) }} 
</script> 
+0

如果您打算让JavaScript的决定,如果该按钮显示与否,可以让按键是可见的所有的时间作为您的用户会能够以任何方式展示它。 – baao

回答

0

使用if(isAdmin)你的做法是正确的。您应该定义为在您的视图,然后做了:

var varButtons = [] 
if(isAdmin){ 
      varButtons = [{ 
       label: 'OVERIDE', 
      cssClass: 'btn-danger', 
      action: function(dialogItself){ 
        checkbox.prop('checked', true); 
        } 
       }]; 
} 
BootstrapDialog.show({ 
     title: 'Age Not Met', 
    message: 'The attendee does not meet the age reguirement for this program.', 
     type: BootstrapDialog.TYPE_WARNING, 
    buttons: varButtons 
相关问题