开发者

ajax solr question

开发者 https://www.devze.com 2022-12-14 05:15 出处:网络
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>

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.

h开发者_运维百科ow come? i have added all the libraries correctly.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消