Click here to Skip to main content
15,904,652 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi people I am using summernote.js and I am not familliar to Jquery and Ajax so much. I am wondering that How can I set mentions option after page loaded with asychronous ajax call(I dont want to wait)? Actually my question is where should I set mentions option and how should ? I am able to retrieve mentions options from controller but setting it in initialization cause problem if I make async call page element doesnt display properly(doesnt initialize) if I make synch call page waits like 1 second and displays then(which I dont want).

What I have tried:

JavaScript
jQuery.extend({
    getMentions: function () {
        $.ajax({
            type: "get",
            dataType: "json",
            async: true,
            url: "getMentions", // "/CreateTicket/getCustomerProjects"
            success: function (data) {
                console.log(data);
                result = data;
                
            }
        });
        //alert(result);
        return result;
    }
});


in document ready
JavaScript
var valset =$.getMentions();
$(".hint2mention").summernote({
  height: 100,
  toolbar: false,
  hint: {
    mentions: valset,
    match: /\B@(\w*)$/,
    search: function (keyword, callback) {
      callback($.grep(this.mentions, function (item) {
        return item.indexOf(keyword) == 0;
      }));
    },
    content: function (item) {
      return '@' + item;
    }    
  }
});
Posted
Updated 25-Apr-16 14:15pm
v3
Comments
Sergey Alexandrovich Kryukov 25-Apr-16 20:06pm    
Please click "Improve question" just to see how code should be formatted (I've done it for you, to make code more readable). Pay attention for "pre" tags.
—SA

1 solution

Here is your mistake: the member success is the function which just does nothing in your code.

You simply assign some variable to some other variable in its body. Even if you have some code which can properly use the result object, there is nothing which would trigger this code.

It looks like you completely misunderstand asynchronous call. If it is asynchronous, at the moment of the statement return result is executed, you don't yet have success called. In other words, result is left uninitialized, or having the value you initialized it with before your call to getMentions. Isn't it obvious?

The solution is simple like hell: the call to success should make it all. You should not return anything, this is useless. The function success should complete all processing, including the side effect, which can be some DOM manipulation. It can call some other function which does it all depending on data. Let's say, you got valid JSON data string in data. Then parse JSON into some data structure, take that data and modify DOM based on it; for example, form some HTML and set it to innerHTML of some element, like div.

See also:
JSON — JavaScript | MDN[^],
Ajax | jQuery API Documentation[^].

—SA
 
Share this answer
 
v2
Comments
Ermany 27-Apr-16 4:39am    
Thank you for your help. Next time I will be more careful.
Sergey Alexandrovich Kryukov 27-Apr-16 9:26am    
Sure. Will you accept the solution formally now? It has all you need.
If you have some doubts, your follow-up questions will be welcome.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900