Skip to content

Instantly share code, notes, and snippets.

@codethug
codethug / koCookieExtender.js
Created July 30, 2019 15:45
A cookie extender for Knockout JS
// Uses the cookie library at https://github.com/js-cookie/js-cookie
// Usage: var firstName = ko.observable().extend({ cookie: "myFirstNameCookie" });
ko.extenders.cookie = function (target, option) {
var cookieName = option;
// Set the observable initially from the cookie value
target(Cookies.get(cookieName));
@codethug
codethug / Example.html
Created June 27, 2019 21:42 — forked from Danielku15/Example.html
A MediaTypeFormatter for WebApi for multipart/form-data including file uploads
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>File Upload example</title>
<link href="/Content/bootstrap.css" rel="stylesheet" />
</head>
<body>
<form action="api/Upload" method="post">
<div class="form-group">
@codethug
codethug / Engineers ViewModel
Last active August 29, 2015 13:57
Testing Durandal Viewmodel that makes http call
define(['plugins/http', 'plugins/dialog', 'durandal/app', 'viewmodels/locale', 'knockout'],
function (http, dialog, app, locale, ko) {
//Note: This module exports an object.
//That means that every module that "requires" it will get the same object instance.
//If you wish to be able to create multiple instances, instead export a function.
//See the "welcome" module for an example of function export.
var engineersViewModel = function() {
var self = this;
/*
* read-only date display with momentjs
* use like this: data-bind="moment: dateVar, format: 'YYYY-MM-DD'"
* The "format" is optional and will default to "MM/DD/YYYY"
*/
ko.bindingHandlers.moment = {
update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var val = valueAccessor();
var date = moment(ko.utils.unwrapObservable(val));
@codethug
codethug / beforeUnloadText.js
Last active December 11, 2015 20:48
A custom binding for KnockoutJS allowing you hook into the onbeforeunload event on the window. This will allow you to display a message to the user when navigating away from or closing the page, reminding the user to save their work, etc.
// Author: Tim Larson @codethug
ko.bindingHandlers.beforeUnloadText = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
if (window.onbeforeunload == null) {
window.onbeforeunload = function(){
var value = valueAccessor();
var promptText = ko.utils.unwrapObservable(value);
if (typeof promptText == "undefined" || promptText == null) {
// Return nothing. This will cause the prompt not to appear
} else {