Skip to content

Instantly share code, notes, and snippets.

View JudahGabriel's full-sized avatar

Judah Gabriel Himango JudahGabriel

View GitHub Profile
class UsersController {
users: User[] = [];
constructor() {
this.fetchUsers();
}
async fetchUsers() {
var result = await this.$http.get("/users/all"); // Put this in a service, obviously. userService.get() or whatever.
this.users = result; // Yay! The UI will automatically update to show these users.
App.run([
"$q",
function ($q: ng.IQService) {
// Use Angular's Q object as Promise. This is needed to make async/await work properly with the UI.
// See http://stackoverflow.com/a/41825004/536
window["Promise"] = $q;
}]);
{
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": false,
"target": "es5",
"strictNullChecks": true,
},
"compileOnSave": true,
// 1. Old, sucky way of doing async work: callback hell!
function myCallback(result) {
this.items = result;
}
someAjaxRequest(myCallback);
// 2. Slightly better way using Promises!
someAjaxRequest().then(results => this.items = results);
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<TypeScriptTarget>ES5</TypeScriptTarget>
<TypeScriptJSXEmit>None</TypeScriptJSXEmit>
<TypeScriptCompileOnSaveEnabled>True</TypeScriptCompileOnSaveEnabled>
<TypeScriptNoImplicitAny>False</TypeScriptNoImplicitAny>
<TypeScriptModuleKind>None</TypeScriptModuleKind>
<TypeScriptRemoveComments>False</TypeScriptRemoveComments>
<TypeScriptOutFile />
<TypeScriptOutDir />
<TypeScriptGeneratesDeclarations>False</TypeScriptGeneratesDeclarations>
// Load up the RecipeViewModels stored in our index.
var recipeViewModel = ravenSession.Query<RecipeViewModel, RecipeViewModelIndex>()
.Where(r => r.Name == "Sweet honey glazed salmon")
.ProjectFromIndexFieldsInto<RecipeViewModel>()
.First();
/// <summary>
/// RavenDB index that is run automatically whenever a Recipe changes. For every recipe, the index outputs a RecipeViewModel.
/// </summary>
public class RecipeViewModelIndex : AbstractIndexCreationTask<Recipe>
{
public RecipeViewModelIndex()
{
Map = allRecipes => from recipe in allRecipes
let chef = LoadDocument<Chef>(recipe.ChefId)
let comments = LoadDocument<Comment>(recipe.CommentIds)
// Listen for changes to Recipes.
// When that happens, update the corresponding RecipeViewModel.
ravenStore
.Changes()
.ForDocumentsOfType<Recipe>()
.Subscribe(docChange => this.UpdateViewModelFromRecipe(docChange.Id));
var recipeViewModel = new RecipeViewModel
{
Name = "Sweet honey glazed salmon",
RecipeId = "recipes/1",
PictureUrl = "http://tastyrecipesyum.com/recipes/1/profile.jpg",
Categories = new List<string> { "salmon", "fish", "seafood", "healthy" },
ChefName = "Swei D. Sheff",
ChefEmail = "borkbork@bork.com",
Comments = new List<Comment> { new Comment { Name = "Dee Liteful", Content = "I really enjoyed this dish!" } },
Ingredients = new List<Ingredient> { new Ingredient { Name = "salmon fillet", Amount = "5 ounce" } }
// Query for all recipes and transform them into RecipeViewModels.
var allRecipeViewModels = ravenSession.Query<Recipe>()
.TransformWith<RecipeViewModelTransformer, RecipeViewModel>()
.ToList();