daytonn
11/24/2014 - 2:23 AM

JSKit autocomplete (naive)

JSKit autocomplete (naive)

/* jshint esnext: true */
var _ = require("lodash");

App.createController("Friends", {
  actions: ["index"],

  all: function() {
    this.cacheElements();
    this.registerEvents();
  },

  index: function() {
  },

  cacheElements: function() {
    this.$completionList = $("#completion-list");
    this.$friendSearchBox = $("#query");
  },

  populateCompletionList: function(responseJSON) {
    this.$completionList.empty();
    _.each(responseJSON.users, function(user) {
      this.$completionList.append("<li>" + user.firstname + " " + user.lastname + "</li>");
    }, this);
  },

  fetchCompletionList: function() {
    $.ajax({
      url: "users/search",
      data: { query: this.$friendSearchBox.val() },
      dataType: "json"
    })
    .done(this.populateCompletionList);
  },

  registerEvents: function() {
    this.$friendSearchBox.on("keyup", this.fetchCompletionList);
  }
});