Skip to content
Harold.Luo edited this page Apr 12, 2014 · 19 revisions

Base usage

$('.atwho-inputor').atwho({
  at: "@",
  data: ["one", "two", "three"],
}).atwho({
  at: ":",
  data: ["+1", "-1", "smile"]
});

Settings

Here are the default settings:

$('.atwho-inputor').atwho({
    // key char for observing such as `@`
    at: void 0,
    /*
        alias name of `at`
        it would be an id attribute of the popup view.
    */
    alias: void 0,
    /*
         should be a plain object *Array* or a *URL*
         would save *Array* directly.
         would load and save remote JSON data by *URL*
     */
    data: null,
    /*
         would eval it and assign value of the key contained in `${}`
         key-value ( {'name': "one"} ) is an item in `data` Array.
    */
    tpl: "<li data-value='${atwho-at}${name}'>${name}</li>",
    /*
         It's for `contentEditable` mode only. Will be evaluated and inserted in the inputor.
         `atwho-data-value` is the value of `data-value` of `li` element, and it is just some `default value hook`.
         You change it into anything you want.
    */
    insert_tpl: "<span>${atwho-data-value}</span>"
    /*
        There are several data processors that can be overriden here such as `filter`.
        we will cover it later.
    */
    callbacks: DEFAULT_CALLBACKS,
    /*
        would matching item by test against the value of this `search_key` with query string.
    */
    search_key: "name",
    /*
        limit number of items to show in popup list.
    */
    limit: 5,
    /*
        setting the max length of the string after `at` that would be matched
        It will stop matching if the query string is longer than `max_len`.
    */
    max_len: 20,
    /*
        if `yes`, At.js will match the query with a space before the `at`.
    */
    start_with_space: yes
    display_timeout: 300
    // highlight_first suggestion in popup menu
    highlight_first: yes
});

Examples

Custom Template

You can customize what will be shown in popup's item , such as user’s avatar.

A valid template should meet the following requirements:

  • It should be a li HTML element
  • It should have the data-value attribute, whose value will be inserted into the inputor if the item is selected.
<li data-value='${atwho-at}${word}'>anything here</li>

You can put any HTML element into the template such as img. Its just a HTML element.

var emojis = ["smile", "iphone", "girl", "smiley", "heart", "kiss", "copyright", "coffee"];
var emojis_list = $.map(emojis, function(value, i) {
  return {'id':i, 'name':value};
});
//http://a248.e.akamai.net/assets.github.com/images/icons/emoji/8.png
$(".container textarea").atwho({
  at: ':',
  tpl: "<li data-value=':${name}:'><img src='http://a248.e.akamai.net/assets.github.com/images/icons/emoji/${name}.png' height='20' width='20'/> ${name} </li>",
  data: emojis_list
});

Register multiple at keys

At.js can listen to multiple at and every Keyword can have its own settings.

var emojis = ["smile", "iphone", "girl", "smiley", "heart", "kiss", "copyright", "coffee"];

var names = ["Jacob", "Isabella", "Ethan", "Emma", "Michael", "Olivia", "Alexander", "Sophia", "William", "Ava", "Joshua", "Emily", "Daniel", "Madison", "Jayden", "Abigail", "Noah", "Chloe", "你好", "你你你"];

var emojis_list = $.map(emojis, function(value, i) {
  return {'id':i, 'name':value};
});

var issues = [
  { name: "1", content: "stay foolish"},
  { name: "2", content: "stay hungry"},
  { name: "3", content: "stay heathly"},
  { name: "4", content: "this happiess"},
];

//http://a248.e.akamai.net/assets.github.com/images/icons/emoji/8.png
$(".container textarea")
  .atwho({
    at: "@",
    data: names
  })
  .atwho({
    at: "#", 
    tpl: '<li data-value="#${name}">${name} <small>${content}</small></li>',
    data: issues
  })
  .atwho({
    at: ":", 
    tpl: "<li data-value=':${name}:'><img src='http://a248.e.akamai.net/assets.github.com/images/icons/emoji/${name}.png' height='20' width='20'/> ${name} </li>",
    data: emojis_list
  });

Load remote data

When you set data as URL string, At.js will launch Ajax request to the server to get the JSON data

   $('textarea').atwho({
        at: "@", 
        data: "http://www.atjs.com/users.json",
        limit: 7
    });

Custom Query matcher

If you want to support unicode characters, you can customize the Query matcher.

The matcher callback look like this:
BTW: You should check Callbacks settings for more details

$('#inputor').atwho({
        at: "@", 
        callbacks: {
            matcher: function(flag, subtext) {
                var match, matched, regexp;
                regexp = new XRegExp('(\\s+|^)' + flag + '(\\p{L}+)$', 'gi');
                match = regexp.exec(subtext);
                // ... get matched result
                return matched;
            }
            //, ... others callbacks
        }
    });

You can read issue #30 for more details.