Forum Discussion

Inactive User's avatar
Inactive User
12 years ago

Jquery in endpoint

This might be a dumb question but I can't seem to use Lithium's jQuery in an endpoint. For instance, I'm trying to create a javascript file with some common js functions we're using in an endpoint but instantiating jquery like I do in a component isn't seeming to work  ie. (function($){//code})(LITHIUM.jQuery); When I try to do anything with $ (in this case, a post), the console shoots back 'TypeError: Cannot call method 'post' of undefined'.

 

Any ideas?

8 Replies

  • AdamN's avatar
    AdamN
    Khoros Oracle
    12 years ago

    When you're testing the endpoint, are you accessing it directly, or are you including it as an external JavaScript file in one of the community pages, or something else perhaps? The important thing to keep in mind about endpoints is that they're entirely stand-alone. None of the core CSS, JS, HTML, etc. is included with endpoints.

     

    So to properly test your endpoint, you'd likely have to include it in one of the community pages and ensure that it's loaded after the core Lithium libraries have loaded (to ensure that the LITHIUM.jQuery object is available at the time you're trying to access it).

  • Can you give us an example of the code you are putting in the end point?

     

    Unlike custom components, custom end points will only return whatever your code outputs - it won't have a HTML header, or includes.This gives you the freedom to output whatever you like (HTML, XML, JSON, ...) but if you want to use JavaScript, you will need to output in HTML, and add the reference to the jQuery library yourself (via the standard <script> tag)..

  • Inactive User's avatar
    Inactive User
    12 years ago

    Sorry, I should've given some more detail. I understand the entire stand-alone nature. I had set the type of endpoint to text/javascript and then included it in the wrapper footer using a script tag, since I thought the Lithium jQuery would have loaded by then. The JS loads fine but when it tries to use $, I come across the error.

     

    Here's the watered down code:

    (function($){
    	test = {
    		url: "https://lithiumdev.domain.com/restapi/vc/search/messages", //used our real domain in the actual code
    
    		getByLabel: function(labelName, callback){
    			var data = { 
    				q: "is_root:true AND labels:"+ labelName
    			}
    
    			$.post(test.url, data, callback);
    		},
    		displayMessageInfo: function(data){
    			console.log(data);
    		}
    	}
    })(LITHIUM.jQuery);

    I call the function test.getByLabel('test',test.displayMessageInfo) and then the error happens: TypeError: Cannot call method 'post' of undefined'.

     

    I may be thinking about this all wrong or it may be something dumb. But, anyways, thanks for the help!

  • nathan's avatar
    nathan
    Executive
    12 years ago

    Not sure why that's not working. Have you checked that LIHTIUM.jQuery is defined when your code is executed (e.g. by debugging)?

     

    You could also try referencing jQuery directly (via $.POST(...) outside the object) to check jQuery has been loaded.

  • Inactive User's avatar
    Inactive User
    12 years ago

    Solution was just to wrap it all in 

    LITHIUM.Loader.onLoad(function(){ //jquery });

     

     

    Thanks guys for the help!

  • Inactive User's avatar
    Inactive User
    12 years ago

    So, this has to turned out to not be as performant as I'd like. Anybody else have any ideas?

  • nathan's avatar
    nathan
    Executive
    12 years ago

    When you say non-performant, do you mean that you have to wait too long for it to load?