Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Topics - Suki
1
Off-topic / Some advice when using jquery.ajax()
« on March 15th, 2013, 01:42 AM »
OK, I found a nice script that pretty much handles mentions in an user friendly way:  @username (it is better than what I had: {username}) the script is: h**p://ichord.github.com/At.js/

The downside is, it requires a previously declared javaScript array for the cript to search the names on, I'm currently using a func to query the database and returns a simple string like {1:'username', 2:'another username'} where the key is the users ID.

Then I pass this string to a JavaScript var by printing the actual string to be handled by the mention script:

Code: [Select]
$context['html_headers'] .= '
<script type="text/javascript"><!-- // --><![CDATA[
var breezeUsers = '. $tools->userMention() .';
// ]]></script>';

This works well, except that it would be a nightmare to print the string if a forum has a huge userbase, of course the query is limited to is_activated and posts > 10 but still it can grow to huge proportions.

What I was thinking is creating a simple SMF action, this action will return the string as a json object, I already had a class that handles all my ajax requests and returns the data as json so adding this wouldn't be an issue, the issue is that no matter how I fetch the string, the mention script doesn't recognize the string as a valid javascript array.

if I sent the data as a json object and use jQuery.parseJSON() on it it screws up the string.

I also tried to serve the response as a JavaScript file since I read that $ajax.() can call and execute javaScript files, however that didn't work either as $ajax.() didn't call the file (console.log told me that the file was fetched and it has a 200 status).

I wanted to use an ajax call because I can control the server response, I can use  some cached entry as response and more importantly, I won't need to print a huge array.

The benefits of using this particular mention script is that it allows me to inject exactly what I want, it is triggered by typing @ or any other character but it can insert back to the textarea whatever you want, I set it up to inject this: @(username, ID)  when an user selects a name from the names list now I have a nice and delimited string that can be parsed and converted to a link easily. No extra queries are needed :)

So, any help handling ajax request would be highly appreciated :) I just basically need a way to retrieve a valid javascript array/object via ajax but all my attempts had failed miserably. Oh, the mention script does support ajax calls, I followed all the exmaples on its doc section but it results in the same errors as my attempts, it doesn't recognize the serve response as a valid array.