Search code examples
jqueryasp.net-mvcimpromptu

Issue in reading resource file in Js file when using BundleTable.EnableOptimizations in MVC


I am using 'jQuery Impromptu' for prompt a user for input, below is the code

$.prompt(Resource.DeleteConfirm, {
  buttons: { 
    [Resource.Res_Option_Yes]: true, 
    [Resource.Res_Option_No]: false 
  },
  submit: function (e, v, m, f) {
    if (!v) {
      return;
    } else {
    }
  }
});

I use a .cshtml file (Resource for reading resource file

<script type="text/javascript">
  var Resource = {
    Res_Option_Yes: '@Resource.res_Option_Yes',
    Res_Option_No: '@Resource.res_Option_No',
    DeleteConfirm: '@Resource.DeleteConfirm'
  };
</script>

When running the application, it will show 'Yes' and 'No' text in buttons in the prompt, but when I enable Optimization (using BundleTable.EnableOptimizations = true) it shows error:

Minification failed. Returning unminified contents.

How can I solve this issue?


Solution

  • The minification failing is as it doesn't like (or possibly understand) the significance of wrapping an inline variable in square brackets so that its value is used as the key of the object.

    To fix this, build the object separately and supply it to the buttons property like this:

    var buttons = {};
    buttons[Resource.Res_Option_Yes] = true;
    buttons[Resource.Res_Option_No] = false;
    
    $.prompt(Resource.DeleteConfirm, {
      buttons: buttons,
      submit: function (e, v, m, f) {
        // note the logic you had in here is redundant...
      }
    });