Skip to content

Instantly share code, notes, and snippets.

@SteveSanderson
SteveSanderson / somefile.cs
Last active May 11, 2016 23:56
Async void message loop
public class MyNetworkClient {
public async Task ConnectAsync(string address) {
await this.MakeTheActualConnection(address);
this.BeginReceiveLoop();
}
// It's async void! But is that bad?
// I know that an unhandled exception here is going to bring down the process, but where else do you want
// that exception to go? There's no external control flow that can reasonably receive such an exception.
private async void BeginReceiveLoop() {
/*! Knockout projections plugin - version 1.1.0-pre
------------------------------------------------------------------------------
Copyright (c) Microsoft Corporation
All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
------------------------------------------------------------------------------
*/
@SteveSanderson
SteveSanderson / StringInterpolatingBindingProvider.js
Created July 8, 2013 11:16
String-interpolating binding provider example for KO 3.0.0 beta. Allows {{ expr }} expressions to insert text into the DOM.
/* --- String interpolating binding provider ---
Allows {{ expr }} syntax in DOM. Could be an official KO plugin
loaded from a separate file. Note that preprocessNode is a fairly
low-level API that developers are not often expected to use directly.
*/
var StringInterpolatingBindingProvider = function() {
this.constructor = StringInterpolatingBindingProvider;
var expressionRegex = /{{([\s\S]+?)}}/g;
// Knockout JavaScript library v2.2.0
// (c) Steven Sanderson - http://knockoutjs.com/
// License: MIT (http://www.opensource.org/licenses/mit-license.php)
(function(){
var DEBUG=true;
(function(window,document,navigator,jQuery,undefined){
!function(factory) {
// Support three module loading scenarios
if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {

##Making the data editable

You're not limited to displaying static data. Let's use the value binding, along with some regular HTML <input> controls, to make the data editable.

Add the following markup to the bottom of your view (leaving the existing markup in place above it):

<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>

Now run the application. What happens when you edit the text in one of the text boxes?

// Knockout JavaScript library v1.3.0ctp
// (c) Steven Sanderson - http://knockoutjs.com/
// License: MIT (http://www.opensource.org/licenses/mit-license.php)
(function(window,undefined){
function b(d){throw d;}var l=void 0,n=null,q=window.ko={};q.b=function(d,e){for(var c=d.split("."),a=window,g=0;g<c.length-1;g++)a=a[c[g]];a[c[c.length-1]]=e};q.j=function(d,e,c){d[e]=c};
q.a=new function(){function d(a,g){if(a.tagName!="INPUT"||!a.type)return!1;if(g.toLowerCase()!="click")return!1;var f=a.type.toLowerCase();return f=="checkbox"||f=="radio"}var e=/^(\s|\u00A0)+|(\s|\u00A0)+$/g,c={click:1,dblclick:1,mousedown:1,mouseup:1,mousemove:1,mouseover:1,mouseout:1,mouseenter:1,mouseleave:1};return{ua:["authenticity_token",/^__RequestVerificationToken(_.*)?$/],o:function(a,g){for(var f=0,c=a.length;f<c;f++)g(a[f])},h:function(a,g){if(typeof a.indexOf=="function")return a.indexOf(g);
for(var f=0,c=a.length;f<c;f++)if(a[f]===g)return f;return-1},Oa:function(a,g,f){for(var c=0,d=a.length;c<d;c++)if(g.call(f,a[c]))return
ko.bindingHandlers.hasFocus = {
init: function(element, valueAccessor) {
$(element).focus(function() {
var value = valueAccessor();
value(true);
});
$(element).blur(function() {
var value = valueAccessor();
value(false);
});
public class VotingController : TaskAsyncController
{
static TaskCompletionSource<bool> hasNewVote = new TaskCompletionSource<bool>();
// Could load this from a DB
static Question question = new Question {
QuestionText = "What's your favourite upcoming technology?",
Answers = new List<Answer> {
new Answer { AnswerId = 1, AnswerText = "C# 5" },
new Answer { AnswerId = 2, AnswerText = "WebSockets" },
@SteveSanderson
SteveSanderson / ko-1.3ctp.debug.js
Created May 4, 2011 09:04
KO with control flow bindings prototype - unminified
// Knockout JavaScript library v1.3.0ctp
// (c) Steven Sanderson - http://knockoutjs.com/
// License: MIT (http://www.opensource.org/licenses/mit-license.php)
(function(window,undefined){
var ko = window["ko"] = {};
// Google Closure Compiler helpers (used only to make the minified file smaller)
ko.exportSymbol = function(publicPath, object) {
var tokens = publicPath.split(".");
var target = window;
@SteveSanderson
SteveSanderson / ko-1.3ctp.js
Created May 4, 2011 09:01
KO with control flow bindings prototype
// Knockout JavaScript library v1.3.0ctp
// (c) Steven Sanderson - http://knockoutjs.com/
// License: MIT (http://www.opensource.org/licenses/mit-license.php)
(function(window,undefined){
function c(d){throw d;}var n=void 0,o=null,p=window.ko={};p.b=function(d,e){for(var b=d.split("."),a=window,f=0;f<b.length-1;f++)a=a[b[f]];a[b[b.length-1]]=e};p.h=function(d,e,b){d[e]=b};
p.a=new function(){function d(a,f){if(a.tagName!="INPUT"||!a.type)return!1;if(f.toLowerCase()!="click")return!1;var h=a.type.toLowerCase();return h=="checkbox"||h=="radio"}var e=/^(\s|\u00A0)+|(\s|\u00A0)+$/g,b={click:1,dblclick:1,mousedown:1,mouseup:1,mousemove:1,mouseover:1,mouseout:1,mouseenter:1,mouseleave:1};return{ha:["authenticity_token",/^__RequestVerificationToken(_.*)?$/],j:function(a,f){for(var h=0,b=a.length;h<b;h++)f(a[h])},g:function(a,f){if(typeof a.indexOf=="function")return a.indexOf(f);
for(var h=0,b=a.length;h<b;h++)if(a[h]===f)return h;return-1},Ba:function(a,f,h){for(var b=0,d=a.length;b<d;b++)if(f.call(h,a[b]))return