Pycharm, Javascript and Ajax Variables

Here’s the problem. You make an AJAX call the returns a Javascript object. Since Pycharm does not know anything about that object, it kicks up a bunch of “Unresolved variable” warnings. Too many to handle with “Suppress for statement”. Too dangerous to use “Suppress for file”. Here’s how to handle it:

$.ajax({type: "POST", url: "my_ajax_url", data: {item1: 7, item2: 3}})
    .done(
        /**
         * @param content
         * @param content.success   indicates ajax code ran successfully
         */
        function (content) {
            if (content.success) {
                alert("Success!")
            }
        }
    );

For more info, see JSDoc documentation. For example, the code below is one way to handle imported libraries

    <script type="text/javascript">
        /**
         * @external google
         * @param google.charts
         * @param google.charts.setOnLoadCallback
         */
        google.charts.load('current', {packages: ['corechart', 'bar']});
        google.charts.setOnLoadCallback(drawChart);

        function drawChart() {
            /**
             * @param google.visualization.DataTable
             * @param google.charts.Bar
             * @param data.addColumn
             * @param data.addRows
             */
            var data = new google.visualization.DataTable();
            data.addColumn('number', 'Hour');
            data.addColumn('number', 'PageViews');

            data.addRows({{ histogram|safe }});

            var options = {
                chart: {
                    title: 'Page Views by Hour'
                },

                axes: {
                    y: {
                        PageViews: {label: 'Page Views'}
                    }
                },
                hAxis: {
                    title: 'Hour of Day'
                }
            };

            // Instantiate and draw our chart, passing in some options.
            var chart = new google.charts.Bar(document.getElementById('chart_div'));
            chart.draw(data, options);
        }
Advertisements

One thought on “Pycharm, Javascript and Ajax Variables

  1. Pingback: Pycharm, Javascript and Ajax Variables — Snakey Code | SutoCom Solutions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s