Last active
January 3, 2016 23:38
-
-
Save IcodeNet/8536020 to your computer and use it in GitHub Desktop.
How do I hook up twitter type ahead and knockoutJS?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//************* 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); | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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