2015-08-28 21 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 
相關問題