Reputation: 797
I'm working with express/node/jade and have build a module for some variables.
I have this line in my app.js app.locals.GVs = require('./utils/globalvars.js');
and this is my jade
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!");
});
});
});
I get an error on when I call GVs var thisUserData = new GVs.UserData(...);
I also tries passing it the same way i did with "data" but it did the same thing.
EDIT: I tried to see if I could get GVs in the jade file like so
p There will hopefully be a form to change users here.
became this
p There will hopefully be a form to change users here. '#{GVs.UserData}'
and its there, why can I get it in jade but not in script.?
Upvotes: 0
Views: 860
Reputation: 3586
You need to pass variables between client-side JS and server-side, because client side don't know about node.js global vars. For example:
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!");
});
});
});
Upvotes: 1