Skip to content

Instantly share code, notes, and snippets.

View danielmackay's full-sized avatar

Daniel Mackay [SSW] danielmackay

View GitHub Profile
@danielmackay
danielmackay / jqButton.js
Created July 7, 2013 11:16
Knockout jQuery button binding
ko.bindingHandlers.jqButton = {
init: function(element) {
$(element).button(); // Turns the element into a jQuery UI button
},
update: function(element, valueAccessor) {
var currentValue = valueAccessor();
// Here we just update the "disabled" state, but you could update other properties too
$(element).button("option", "disabled", currentValue.enable === false);
}
};
@danielmackay
danielmackay / starbinding.js
Created July 7, 2013 11:17
Knockout startbinding.
ko.bindingHandlers.starRating = {
init: function(element, valueAccessor) {
$(element).addClass("starRating");
for (var i = 0; i < 5; i++)
$("<span>").appendTo(element);
// Handle mouse events on the stars
$("span", element).each(function(index) {
$(this).hover(
function() { $(this).prevAll().add(this).addClass("hoverChosen") },
@danielmackay
danielmackay / formpost.html
Last active December 19, 2015 10:49
Posting JSON with Knockout.
<form action="/tasks/saveform" method="post">
<hidden name="tasks" data-bind="value: ko.toJSON(tasks)"></hidden >
<button type="submit">Save</button>
</form>
@danielmackay
danielmackay / DumpBinding.js
Last active December 19, 2015 11:38
Knockout dump binding handler.
ko.bindingHandlers.dump = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext){
var context = valueAccessor();
var allBindings = allBindingsAccessor)();
var pre = document.createElement('pre');
element.appendChild(pre);
var dumpJSON = ko.computed({
read: function(){
@danielmackay
danielmackay / datePickerFormat.js
Created July 19, 2013 05:45
jQuery UI datepicker format issues in chrome. Add this after you have configured the datepicker to avoid date format problems.
jQuery.validator.addMethod(
'date',
function (value, element, params) {
if (this.optional(element)) {
return true;
};
var result = false;
try {
$.datepicker.parseDate('dd/mm/yy', value);
result = true;
@danielmackay
danielmackay / ApplyAseemblyVersions.ps1
Last active December 20, 2015 07:29
Apply version to assemblies from TF Build. #PS
# ApplyVersionToAssemblies.ps1
#
# Look for a 0.0.0.0 pattern in the build number.
# If found use it to version the assemblies.
#
# For example, if the 'Build number format' build process parameter
# $(BuildDefinitionName)_$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)
# then your build numbers come out like this:
# "Build HelloWorld_2013.07.19.1"
# This script would then apply version 2013.07.19.1 to your assemblies.
@danielmackay
danielmackay / Wrap.cs
Created July 30, 2013 23:22
Wrap function. Good way to have a generic exception handler.
private static void Wrap(Action func)
{
try
{
func();
}
catch (DbEntityValidationException ex)
{
var error = ex.EntityValidationErrors.First().ValidationErrors.First();
Debug.WriteLine(error.PropertyName + ": " + error.ErrorMessage);
@danielmackay
danielmackay / DocumentGenerator.cs
Created August 8, 2013 05:11
PDF generator from master Word file.
public class DocumentGenerator
{
public void CreatePdf(string originalPath, string outputPath, IEnumerable<TextInfo> replacementText)
{
// Create a new Microsoft Word application object
var word = new Application();
// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = System.Reflection.Missing.Value;
@danielmackay
danielmackay / SocialUrl.cs
Created August 8, 2013 12:42
Social URL generator for Twiiter, Facebook, and Google+. #MVC.
public static class SocialUrl
{
public static string LocalUrl { get { return HttpContext.Current.Request.Url.AbsoluteUri; } }
public static string TwitterUrl(string postTitle = null, string twitterHandle = null)
{
var sb = new StringBuilder("https://twitter.com/intent/tweet?url=" + LocalUrl);
if (!string.IsNullOrWhiteSpace(postTitle))
sb.AppendFormat("&text={0}", postTitle);
@danielmackay
danielmackay / ListExt.cs
Created August 20, 2013 05:47
List helpers. C#.
public static class ListExt
{
public static bool IsNullOrEmpty<T>(this IList<T> list)
{
return (list == null || list.Count == 0);
}
}