JsHint Essentials Web Prevention "ko undefined" warning when using knockout

In an ASP.NET MVC 5 project using binding and minimization, I have a Javascript view model that I populate in a .cshtml file. The view model refers to a knockout via ko , which works just fine. However, the JsHint output that comes from Web Essentials reports W117 warnings, 'ko' is not defined for every link to ko .

The .js files are as follows:

 /* exported MyViewModel */ function MyViewModel(viewModel) { self.someValue = ko.observable(); // JsHint warning on this line. ... } 

The .cshtml files are as follows:

 ... @section Scripts { <script> ko.applyBindings(new MyViewModel(ko.mapping.fromJS(@Html.Raw(Json.Encode(Model))))); </script> } 

How can I keep the benefits of "undefined" warnings at all, but avoid these false warnings?

+7
jshint visual-studio-2013 web-essentials
source share
2 answers
  • From the Web Essentials menu, select Change jshint global settings
  • Scroll down the .jshintrc file and add the following:

     "globals" : { "ko": false} // additional predefined global variables 

This will prevent jshint from complaining about ko, but still warn you about other undefined characters.

Note that you can also do this for each file by putting this comment at the top of your javascript file.

 /*global ko*/ 
+24
source share

New Web Essentials can read .jshintrc from parent directories.

  • Select Change JSHint Global Settings (.jshintrc) in the Web Essentials menu.
  • Copy the contents of the file to the .jshintrc file in the solution folder.
  • Edit the file as needed (e.g. add ko as a global variable)

Thus, the .jshintrc file can be added to the version control to share it with all developers without having to edit the global JSHint settings on separate workstations.

+4
source share

All Articles