var in...">

How to tell PhpStorm that a global javascript variable is defined?

I have a branch template with this:

<script type="text/javascript">
  var initParams = {
    homePage: '{{ url('_homepage') }}'
  };
</script>

      

On the other hand, I have a .js file using initParams.homePage. The problem is that PHPStorm thinks my initParams are undefined. Is there a way to send inTwig js variable protection to the global scope of PHPStorm?

Thanks in advance!

+3


source to share


2 answers


When using jslint to check Javascript, you can use something like this to avoid false warnings:

/*global $, alert, confirm, initParams */
/*jslint browser:true */
$(document).ready(function () {
    alert(initParams.homepage);
});

      



enter image description here

By the way, if you want to use routing in your Javascript code, you should take a look at FOSJsRoutingBundle .

+11


source


I just found a way to avoid phpstorm "unresolved variables" complaints about a global javascript variable defined inside an HTML file.

For example, if you have this global variable:

<script type="text/javascript">
  var initParams = {
    homePage: '{{ url('_homepage') }}'
  };
</script>

      

Then in another js project file (ex: phpstorm_definitions.js) declare a "fake" variable like this:



/* fake variables: used only to avoid phpstorm complaints */
var initParams = {};
var initParams.homePage = '';   

      

If the file is inside your project folder, you don't need to include the phpstorm_definitions.js file anywhere. Now you can use your constant in all other files and phpstorm will no longer complain that the constant is undefined.

This is just a trick but works well for me

+1


source







All Articles