我正在使用express / node / jade,并为一些变量构建了一个模块。
我的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!");
});
});
});
当我致电GV var thisUserData = new GVs.UserData(...);
我也尝试以与“数据”相同的方式传递它,但它做了同样的事情。
编辑:我试着看看我是否能像玉石文件中那样获得GV
p There will hopefully be a form to change users here.
成为了这个
p There will hopefully be a form to change users here. '#{GVs.UserData}'
它在那里,为什么我可以用玉器而不是脚本来获取它。?
答案 0 :(得分: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!");
});
});
});