Skip to content

Instantly share code, notes, and snippets.

@ssajous
ssajous / bootstrapShowModal.js
Last active April 12, 2018 13:39
Knockout.js binding handle to hide and show bootstrap modal dialog. jsFiddle example http://jsfiddle.net/shub2079/TAtvU/
// Custom binding for modal dialog
// Bind a Bootstrap modal div to an observable
// boolean. When the observable changes, the
// modal window's visibility will also change.
//
// The binding is two-way, so changes to the
// modal window's visibility will also change
// the bound observable's value
ko.bindingHandlers.bootstrapShowModal = {
init: function (element, valueAccessor) {
@ssajous
ssajous / GenericRepository.cs
Last active December 19, 2015 22:48
Entity Framework generic repository. Meant to be used as a wrapper to access a particular type/table from DbContext
public class GenericRepository<T> : IRepository<T> where T : class
{
protected DbSet<T> DBSet {get;set;}
protected DbContext Context { get; set; }
public GenericRepository(DbContext context)
{
if (context == null)
{
throw new ArgumentException("An instance of DbContext is " +
@ssajous
ssajous / validationUtility.js
Created July 15, 2013 14:26
Validation utility to be used with Twitter Bootstrap. Displays tooltip on the first invalid element in the form, as well as highlights all invalid elements.
var ValidationUtility = function() {
var validationElements = $('[data-role="validate"]');
var elementCount;
validationElements.popover({
placement: 'top'
});
validationElements.on('invalid', function() {
if (elementCount === 0) {
@ssajous
ssajous / Error Validation - CustomGlobalConfig.cs
Last active December 19, 2015 16:59
Validation Filter to send a JSON response with validation errors when an HTTP Request contains an invalid data model. Uses JSON.net.
using Newtonsoft.Json.Serialization;
using System.Web.Http;
using System.Web.Http.Validation.Providers;
using MyApplication.Web.Filters;
namespace MyApplication.Web
{
public static class CustomGlobalConfig
{
public static void Customize(HttpConfiguration config)
@ssajous
ssajous / KOMultiselectBinding.js
Last active June 13, 2016 21:37
Knockout.js binding handler for the JQuery UI multiselect widget
ko.bindingHandlers.multiselect = {
init: function (element, valueAccessor, allBindingAccessors) {
"use strict";
var options = valueAccessor();
ko.bindingHandlers.options.update(
element,
function() { return options.options; },
allBindingAccessors
@ssajous
ssajous / Dice.cs
Last active October 14, 2021 15:41
Implementations of Dice's Coefficient used to get a similarity index between two strings.
public static double DiceCoefficient(string stOne, string stTwo)
{
HashSet<string> nx = BuildBigramSet(stOne);
HashSet<string> ny = BuildBigramSet(stTwo);
HashSet<string> intersection = new HashSet<string>(nx);
intersection.IntersectWith(ny);
double dbOne = intersection.Count;
return (2 * dbOne) / (nx.Count + ny.Count);