You're initializing myData
in your define()
call with whatever value is in sessionStorage['username']
. Therefore, the first time you call define()
the value is null, and is not set until you initialize sessionStorage['username']
in your require()
function.
Instead of initializing myData
to a string, make it a getter function that retrieves the current value at invocation:
define('environment', {
myData: function() { return 'http://nothing&username=' + sessionStorage.getItem('username'); }
});
require(['environment'], function(environment) {
sessionStorage['username'] = 'theUsername';
$("ul").append('<li>' + sessionStorage.getItem('username') +'</li>');
$("ul").append('<li>' + environment.myData() +'</li>');
});