I am using ASP.NET MVC and I have a simple requirement. I need to be able to "inject" two server side values into my JavaScript object.
My JavaScript object lives in a separate js file referenced by my project BUT I need to be able to inject data into my object prior to it being used for the first time.
My code currently looks like this.
var ImageSizeEnum = {
Small: 0,
Medium: 1,
Large: 2
};
var UrlBuilder = (function (baseImageUrl, storageContainer) {
var _baseImageUrl = baseImageUrl;
var _storageContainer = storageContainer;
this.BuildImageUrl = function (stockImage, imageSizeEnum) {
var imageSizeString = "";
switch (imageSizeEnum) {
case ImageSizeEnum.Small:
imageSizeString = "sm";
break;
case ImageSizeEnum.Medium:
imageSizeString = "md";
break;
case ImageSizeEnum.Large:
imageSizeString = "lg";
break;
}
var url = kendo.format(_baseImageUrl + "_{2}{3}", _storageContainer, stockImage.AzureId, imageSizeString, stockImage.Extension);
return url;
};
});
and it is invoked in my _Layout.cshtml page like this.
$(function () {
UrlBuilder("@WebConfigSettings.BaseImageUrlPath", "@WebConfigSettings.AzureStorageContainer");
});
But this code doesn't appear to work as I expected due to BuildImageUrl being undefined. I assume this is because I am not actually initializing and returning my UrlBuilder object by invoking it with () at declaration.
I am somewhat aware of the different types of JavaScript object patterns (i.e. literals, function etc) but I am completely uncertain about what the 'correct' pattern and approach is to solve my particular problem.
Any suggestions?