2015-04-30 15 views
0

我正在使用express/node/jade併爲某些變量構建模塊。爲什麼我不能在玉器中找到app.locals

我在app.js app.locals.GVs = require('./utils/globalvars.js');

這條線,這是我的玉

extends ../layout 

block content 
    h1 Edit - Change Your Profile 
    p There will hopefully be a form to change users here. 
    form(id='form',action='') 
    include ./includes/user-form.jade 
    input(type='reset', value='Clear') 
    input(type='submit', value='Update') 
    script. 
    //Wait for document to load. 
    $(document).ready(function(){ 
     $('#firstname').val('#{data.firstname}'); 
     $('#lastname').val('#{data.lastname}'); 
     $('#username').val('#{data.username}'); 
     $('#phone').val('#{data.phone}'); 
     $('#email').val('#{data.email}'); 

     //Once form is submitted thisUserData (UserData) is created. 
     $('#form').submit(function(){ 
     // creates the variable thisUserData as a new UserData. 
     var thisUserData = new GVs.UserData($('#firstname').val(), $('#lastname').val(), $('#username').val(), $('#password').val(), $('#confirmpassword').val(), $('#phone').val(), $('#email').val()); 
     //POST call; Sends thisUserData and alerts that a user was updated. 
     $.post('/user/edit', {userFormData: thisUserData}, function(data){ 
      alert("User updated!"); 
     }); 
     }); 
    }); 

我,當我打電話GVS var thisUserData = new GVs.UserData(...);

我也試圖通過它上得到一個錯誤與「數據」相同的方式,但它做了同樣的事情。

編輯:我想看看我是否能在玉文件中獲取GVS像這樣

p There will hopefully be a form to change users here. 

成爲本

p There will hopefully be a form to change users here. '#{GVs.UserData}' 

和它的存在,我爲什麼能得到它在玉,但不在腳本中?

回答

1

您需要在客戶端JS和服務器端之間傳遞變量,因爲客戶端並不知道node.js全局變量。例如:

extends ../layout 

block content 
    h1 Edit - Change Your Profile 
    p There will hopefully be a form to change users here. 
    form(id='form',action='') 
    include ./includes/user-form.jade 
    input(type='reset', value='Clear') 
    input(type='submit', value='Update') 
    script. 
    var GVs = !{JSON.stringify(GVs)}; 
    //Wait for document to load. 
    $(document).ready(function(){ 
     $('#firstname').val('#{data.firstname}'); 
     $('#lastname').val('#{data.lastname}'); 
     $('#username').val('#{data.username}'); 
     $('#phone').val('#{data.phone}'); 
     $('#email').val('#{data.email}'); 

     //Once form is submitted thisUserData (UserData) is created. 
     $('#form').submit(function(){ 
     // creates the variable thisUserData as a new UserData. 
     var thisUserData = new GVs.UserData($('#firstname').val(), $('#lastname').val(), $('#username').val(), $('#password').val(), $('#confirmpassword').val(), $('#phone').val(), $('#email').val()); 
     //POST call; Sends thisUserData and alerts that a user was updated. 
     $.post('/user/edit', {userFormData: thisUserData}, function(data){ 
      alert("User updated!"); 
     }); 
     }); 
    }); 
+0

這適用於我在該模塊中具有的變量,但無法訪問其中的任何函數。 – Devcon

+0

@Devcon如果你想在客戶端和服務器上使用相同的功能,你可以使用browserify http://browserify.org/ – vanadium23

+0

感謝@ vanadium23的幫助我使用了webpack。 – Devcon

相關問題