views:

329

answers:

2

i followed the ajax solr tutorial, and in step one i wrote this code.

header.php:

<script type="text/javascript" src="static/js/ajax-solr/core/Core.js"></script>
<script type="text/javascript" src="static/js/ajax-solr/core/AbstractManager.js"></script>
<script type="text/javascript" src="static/js/ajax-solr/managers/Manager.jquery.js"></script>
<script type="text/javascript" src="static/js/ajax-solr/core/Parameter.js"></script>
<script type="text/javascript" src="static/js/ajax-solr/core/ParameterStore.js"></script>
<script type="text/javascript" src="static/js/reuters.js"></script>

reuters.js:

var Manager;

(function ($) {
  $(function () {
    Manager = new AjaxSolr.Manager({
      solrUrl: 'http://localhost/solr/select'
    });
    Manager.init();
  });
})(jQuery);

// build query
Manager.store.addByValue('q', '*:*');

// do request
Manager.doRequest();

but when i run it firebug says Manager.store.addByValue('q', ':'); is not defined.

how come? i have added all the libraries correctly.

+1  A: 

I guess your script should more look like this.

In your case the last to statements are outside of the "ready" function and may thus run before the AjaxSolr libraries have finished loading.

var Manager;

(function ($) {

  $(function () {
    Manager = new AjaxSolr.Manager({
      solrUrl: 'http://example.solrstuff.org/solrjs/select'
    });
    Manager.init();
    //moved the next two calls inside the initialization block
    Manager.store.addByValue('q', '*:*');
    Manager.doRequest();
  });

})(jQuery);


To answer the question from the comment (not related to actual question so just skip if you understand the code yourself)

The inner-one $(function (){...}); is just a shorthand for $(document).ready(function (){...});.

The outer-one:(function($){ })(jQuery); defines an anonymous unnamed function function($){ } with a parameter and immediately calls the function passing in the jQuery object as the parameter. So inside the $ is the jQuery object. I guess they use this syntax as AjaxSolr is framework agnostic and when you use some other framework you just replace the (...)(jQuery) with (...)(otherFrameworkObjectInstance)

So is only (almost) a fancier version of the following

var Manager
var myFunction = function ($) {
    $(function () {
        Manager = new AjaxSolr.Manager({
            solrUrl: 'http://example.solrstuff.org/solrjs/select'
        });
        Manager.init();
        Manager.store.addByValue('q', '*:*');
        Manager.doRequest();
    });
};

myFunction(jQuery);

But this leaves you with a useless variable myFunction which refers to a function you only run once anyway. And this style is also similar to what the jQuery Plugin best practices looks like

jitter
thx it worked. what does (function($){ })(jQuery); do? i have just seen $(document).ready(function(){ });
weng
check second part of answer
jitter
thx for the answer!
weng
oops...i wanted to raise your point but instead you dropped it:(
weng
You can't vote twice for an answer AFAIK. If you click a second time you "take back" your vote. so try clicking a "third" time to see if you can restore the vote
jitter
cant..you have to edit your answer..then i can vote again=) so do it...
weng
Did as requested ;)
jitter
A: 

Regarding the (function($){ })(jQuery); syntax, I use this syntax so that I may safely use the $ variable inside the function. Using a different JS framework would probably require more modifications than simply substituting jQuery for otherFramework, depending on how you've been using the $ variable within the function.

James McKinney