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.
Best Answer
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.
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 functionfunction($){ }
with a parameter and immediately calls the function passing in the jQuery object as the parameter. So inside the$
is thejQuery
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
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