Skip to content

Instantly share code, notes, and snippets.

@IcodeNet
Last active January 3, 2016 23:38
Show Gist options
  • Save IcodeNet/8536020 to your computer and use it in GitHub Desktop.
Save IcodeNet/8536020 to your computer and use it in GitHub Desktop.
How do I hook up twitter type ahead and knockoutJS?
//************* HTML *****************************
<input class="typeahead form-control"
type="text"
placeholder="Fnet User Lookup">
//************* JS *****************************
$('.typeahead').typeahead({
remote: {
url: myApp.rootPath + 'user/AutocompleteUser?query=%query',
wildcard: '%query'
},
limit: 10,
});
// where viewModel our ViewModel
$(".typeahead").on("typeahead:selected", function (e, datum) {
viewModel.userToAdd(datum.value);
});
/* an MVC Controller */
public JsonResult AutocompleteUser(string query)
{
if (!string.IsNullOrEmpty(query))
{
MembershipUserCollection userCollection = Membership.FindUsersByName(query);
var results = userCollection.Cast<MembershipUser>().Select(x => new TypeaheadResult
{
tokens = new[] { x.Email, x.UserName },
value = x.Email,
//userId = ToBeSetOnINSERT
});
return this.Json(results, JsonRequestBehavior.AllowGet);
}
return this.Json(new { }, JsonRequestBehavior.AllowGet);
}
public class TypeaheadResult
{
#region Public Properties
/// <summary>
/// Gets or sets the users name.
/// </summary>
public string[] tokens { get; set; }
/// <summary>
/// Gets or sets the login email.
/// </summary>
public string value { get; set; }
public int userId { get; set; }
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment