<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Splunk 6 w/ Django -- Getting JQuery not defined javascript error in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/Splunk-6-w-Django-Getting-JQuery-not-defined-javascript-error/m-p/122046#M1787</link>
    <description>&lt;P&gt;This is not necessarily hacky. A different way to do the above is just to use nested require calls.&lt;/P&gt;

&lt;P&gt;So it might look something like:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;require(["jquery"], function() {
    require(["/path/to/grid.locale.js"], function() {
        require(["/path/to/jqgrid.js"], function() {
            // Use $.grid here
        });
    });
})
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;That's not a super uncommon require-approach for non-AMD scripts.&lt;/P&gt;</description>
    <pubDate>Wed, 30 Oct 2013 22:30:07 GMT</pubDate>
    <dc:creator>ineeman</dc:creator>
    <dc:date>2013-10-30T22:30:07Z</dc:date>
    <item>
      <title>Splunk 6 w/ Django -- Getting JQuery not defined javascript error</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Splunk-6-w-Django-Getting-JQuery-not-defined-javascript-error/m-p/122042#M1783</link>
      <description>&lt;P&gt;Hey Splunk Gurus, &lt;/P&gt;

&lt;P&gt;I am writing a custom Splunk 6 application using Django bindings.  Basically, it's similar to the Music App tutorial.&lt;/P&gt;

&lt;P&gt;Anyhow -- I'm loading in a third-party javascript plugin that uses jQuery. It's a jquery data grid called jqgrid&lt;/P&gt;

&lt;P&gt;When I try to display a data grid on a template, I get a "Uncaught ReferenceError: jQuery is not defined" &lt;/P&gt;

&lt;P&gt;Here is a screenshot: imgur.com/LpWCrTD&lt;/P&gt;

&lt;P&gt;Now, my experience tells me jquery isn't loaded when this javascript gets called. I looked at other examples (and plugins) and they seems to works. So what's going on here? When is jquery loaded into page? &lt;/P&gt;

&lt;P&gt;Here is my code: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{% extends "splunkdj:base_with_app_bar.html" %}

{% load splunkmvc %}

{% block title %}Edit Profiles{% endblock title %}
{% block css %}
    &amp;lt;link rel="stylesheet" type="text/css" href="{{STATIC_URL}}{{app_name}}/custom.css" /&amp;gt;
    &amp;lt;link rel="stylesheet" type="text/css" href="{{STATIC_URL}}splunkjs/css/dashboard.css" /&amp;gt;

    &amp;lt;link rel="stylesheet" type="text/css" media="screen" href="{{STATIC_URL}}{{app_name}}/ui-lightness/jquery-ui.css" /&amp;gt;
    &amp;lt;link rel="stylesheet" type="text/css" media="screen" href="{{STATIC_URL}}{{app_name}}/jqGrid/css/ui.jqgrid.css" /&amp;gt; 

{% endblock css %}

{% block content %}
    &amp;lt;div style="padding: 50px"&amp;gt;
        &amp;lt;p&amp;gt;Hello World&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;

    {% table id="table_searchresults" managerid="search_resulttable" %}
    {% chart id="chart_sourcetype" managerid="search_chart" type="pie" %}
{% endblock content%}

{% block managers %}
    {% searchmanager
        id="search_resulttable"
        search="index=_internal sourcetype=splunkd | head 3"
        cache=True
    %}
    {% searchmanager
        id="search_chart"
        search="index=_internal | head 1000 | stats count by sourcetype"
        cache=True
    %}
{% endblock managers %}

{% block js %}
&amp;lt;script src="{{STATIC_URL}}{{app_name}}/jqGrid/js/i18n/grid.locale-en.js" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src="{{STATIC_URL}}{{app_name}}/jqGrid/js/jquery.jqGrid.src.js" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;
{% endblock js %}
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Oct 2013 13:58:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Splunk-6-w-Django-Getting-JQuery-not-defined-javascript-error/m-p/122042#M1783</guid>
      <dc:creator>joshuamcqueen</dc:creator>
      <dc:date>2013-10-30T13:58:48Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk 6 w/ Django -- Getting JQuery not defined javascript error</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Splunk-6-w-Django-Getting-JQuery-not-defined-javascript-error/m-p/122043#M1784</link>
      <description>&lt;P&gt;Good question, and the answer is a bit subtle.&lt;/P&gt;

&lt;P&gt;We load very few of our JS dependencies using script tags, mostly because it &lt;BR /&gt;
quickly becomes problematic to work out all the dependencies, especially as we&lt;BR /&gt;
don't know what components you're going to load on a page.&lt;/P&gt;

&lt;P&gt;Given that, we use &lt;CODE&gt;require.js&lt;/CODE&gt; (&lt;A href="http://requirejs.org/"&gt;http://requirejs.org/&lt;/A&gt;) to do our dependency&lt;BR /&gt;
management and loading. That's why you see all these instances in the examples&lt;BR /&gt;
of doing things like:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;require(['splunkjs/mvc/chartview'], function(ChartView) { ... });
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;However, this makes things a bit interesting in your case, where you want to&lt;BR /&gt;
load something that depends on jQuery (jqgrid, in your case). The reason you're&lt;BR /&gt;
getting the error is that when your &lt;CODE&gt;&amp;lt;script&amp;gt;&lt;/CODE&gt; tags get executed, jQuery hasn't&lt;BR /&gt;
loaded yet, because it hasn't finished being required. As such, when jqgrid tries&lt;BR /&gt;
to reference the &lt;CODE&gt;jQuery&lt;/CODE&gt; global variable, it can't.&lt;/P&gt;

&lt;P&gt;So what's the solution? There are a couple of options:&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;&lt;P&gt;You can use &lt;CODE&gt;require.js&lt;/CODE&gt; to load jqgrid. This would probably mean doing a&lt;BR /&gt;
&lt;CODE&gt;require.config&lt;/CODE&gt; call in your page to define the paths (and possibly the shim&lt;BR /&gt;
definitions). This might look like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;require.config({
    paths: {
        "jqgrid.locale": "{{STATIC_URL}}{{app_name}}/jqGrid/js/i18n/grid.locale-en.js",
        "jqgrid": "{{STATIC_URL}}{{app_name}}/jqGrid/js/jquery.jqGrid.src.js"
    },
    shim: {
        "jqgrid.locale": {
            deps: ["jquery"]
        },
        "jqgrid": {
            deps: ["jquery", "grid.locale"],
            exports: "jQuery.fn.jqGrid"
        }
    }
});

require(["jqgrid"], function(jqgrid) {
    // ...
});
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I don't know that the above code is 100% correct, but it's a rough estimation&lt;BR /&gt;
of what this might look like in a &lt;CODE&gt;require&lt;/CODE&gt;-ified way.&lt;/P&gt;

&lt;P&gt;One interesting note is that you are going to be dependent on the JS Stack's&lt;BR /&gt;
version &lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;Another option might be to include your own version of jQuery, and then when&lt;BR /&gt;
the rest of your dependencies are loaded, you can call &lt;CODE&gt;noConflict()&lt;/CODE&gt; on it. This&lt;BR /&gt;
might look like:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;script src="{{STATIC_URL}}{{app_name}}/jquery.js" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src="{{STATIC_URL}}{{app_name}}/jqGrid/js/i18n/grid.locale-en.js" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src="{{STATIC_URL}}{{app_name}}/jqGrid/js/jquery.jqGrid.src.js" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script&amp;gt;
    jQuery.noConflict();
&amp;lt;/script&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;That way, you know you can always rely on your version of jQuery, and also that&lt;BR /&gt;
only the jqgrid code will have access to that jQuery reference.&lt;/P&gt;&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;Hopefully the above options make sense. Let me know if I can expand or explain&lt;BR /&gt;
something further. Also, we always love to hear what people are building, so &lt;BR /&gt;
feel free to share &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2013 18:12:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Splunk-6-w-Django-Getting-JQuery-not-defined-javascript-error/m-p/122043#M1784</guid>
      <dc:creator>ineeman</dc:creator>
      <dc:date>2013-10-30T18:12:47Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk 6 w/ Django -- Getting JQuery not defined javascript error</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Splunk-6-w-Django-Getting-JQuery-not-defined-javascript-error/m-p/122044#M1785</link>
      <description>&lt;P&gt;I actually solved this in a very hacky way. Basically I wait until splunk is ready, then I use a jquery getScript() to pull in the external scripts. &lt;/P&gt;

&lt;P&gt;Now, if you look closely you'll notice they are nested. This is because there's AJAX taking place, and I need to wait for everything to come back before I can start using it. Nesting the functions() solves this. &lt;/P&gt;

&lt;P&gt;Although it works, this is definitely not the best approach. Requirejs (like ineeman mentioned above) seems a lot cleaner. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;script&amp;gt;
require(["splunkjs/ready!"], function(mvc) {   

    //get the scripts using jquery
    $.getScript( "/dj/static/josh_app/jqGrid/js/i18n/grid.locale-en.js", function( data, textStatus, jqxhr ) {
    console.log( "grid.locale-en.js load was performed." );


        $.getScript( "/dj/static/josh_app/jqGrid/js/jquery.jqGrid.min.js", function( data, textStatus, jqxhr ) {
        console.log( "jquery.jqGrid.min.js load was performed." );

        console.log("about to access the grid"); 
        //the rest of this is all jqGrid stuff 
        jQuery("#list2").jqGrid({
            url:'http://172.16.127.128:8000/dj/en-us/josh_app/ajax-request',
            datatype: "json",
            colNames:['ID','User', 'Date', 'Profile Name','Low Warn','Low Error','High Warn','High Error'],
            colModel:[
                {name:'id',index:'id'},
                {name:'userid',index:'userid'},
                {name:'date',index:'date'},
                {name:'profile_name',index:'profile_name'},
                {name:'low_warn',index:'low_warn'},     
                {name:'low_error',index:'low_error'},   
                {name:'high_warn',index:'high_warn'},   
                {name:'high_error',index:'high_error'}                                                          
            ],
            rowNum:10,
            rowList:[10,25,50],
            pager: '#pager2',
            sortname: 'id',
            viewrecords: true,
            sortorder: "desc",
            caption:"Threshold Profiles"
        });

        jQuery("#list2").jqGrid('navGrid','#pager2',{edit:false,add:false,del:false});        

        });       
    }); 
});
&amp;lt;/script&amp;gt;   
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Oct 2013 22:20:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Splunk-6-w-Django-Getting-JQuery-not-defined-javascript-error/m-p/122044#M1785</guid>
      <dc:creator>joshuamcqueen</dc:creator>
      <dc:date>2013-10-30T22:20:51Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk 6 w/ Django -- Getting JQuery not defined javascript error</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Splunk-6-w-Django-Getting-JQuery-not-defined-javascript-error/m-p/122045#M1786</link>
      <description>&lt;P&gt;Good call on the requirejs.  I knew splunk was using it, but I didn't quite understand the syntax.  &lt;/P&gt;

&lt;P&gt;I'm building a sweet-sweet data gird (with a SQLite backedn) just like -&amp;gt; &lt;A href="http://trirand.com/blog/jqgrid/jqgrid.html"&gt;http://trirand.com/blog/jqgrid/jqgrid.html&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;I'll write up a blog post once it's done.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2013 22:23:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Splunk-6-w-Django-Getting-JQuery-not-defined-javascript-error/m-p/122045#M1786</guid>
      <dc:creator>joshuamcqueen</dc:creator>
      <dc:date>2013-10-30T22:23:15Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk 6 w/ Django -- Getting JQuery not defined javascript error</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Splunk-6-w-Django-Getting-JQuery-not-defined-javascript-error/m-p/122046#M1787</link>
      <description>&lt;P&gt;This is not necessarily hacky. A different way to do the above is just to use nested require calls.&lt;/P&gt;

&lt;P&gt;So it might look something like:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;require(["jquery"], function() {
    require(["/path/to/grid.locale.js"], function() {
        require(["/path/to/jqgrid.js"], function() {
            // Use $.grid here
        });
    });
})
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;That's not a super uncommon require-approach for non-AMD scripts.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2013 22:30:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Splunk-6-w-Django-Getting-JQuery-not-defined-javascript-error/m-p/122046#M1787</guid>
      <dc:creator>ineeman</dc:creator>
      <dc:date>2013-10-30T22:30:07Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk 6 w/ Django -- Getting JQuery not defined javascript error</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Splunk-6-w-Django-Getting-JQuery-not-defined-javascript-error/m-p/122047#M1788</link>
      <description>&lt;P&gt;Awesome, really looking forward to seeing it.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2013 22:30:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Splunk-6-w-Django-Getting-JQuery-not-defined-javascript-error/m-p/122047#M1788</guid>
      <dc:creator>ineeman</dc:creator>
      <dc:date>2013-10-30T22:30:56Z</dc:date>
    </item>
  </channel>
</rss>

