Skip to content

Instantly share code, notes, and snippets.

View MatthewBarker's full-sized avatar

Matt Barker MatthewBarker

  • Cheadle, Cheshire, England
View GitHub Profile
@MatthewBarker
MatthewBarker / jsonp.js
Created February 18, 2015 14:07
JSONP request function
/*jslint browser: true*/
/*global define, exports, module*/
/**
A module that makes jsonp requests.
@module jsonp
@author Matt Barker
*/
(function (factory) {
'use strict';
@MatthewBarker
MatthewBarker / wikipedia-jsonp.js
Created February 18, 2015 14:14
Make JSONP requests to the Wikipedia API
/*jslint browser: true*/
/*global define, exports, require, module, jsonp*/
/**
A module for Jsonp requests to the Wikipedia API.
@module wikipedia-jsonp
@author Matt Barker
@requires jsonp
*/
(function (factory) {
@MatthewBarker
MatthewBarker / array-spec.js
Created February 24, 2015 15:43
Jasmine 2.2.0 + Require.js: How to use Jasmine's boot file
/*global describe , it, expect*/
describe('Array', function () {
'use strict';
describe('.push()', function () {
it('should append a value', function () {
var arr = [];
arr.push('foo');
@MatthewBarker
MatthewBarker / bdd-feature.js
Created February 26, 2015 15:38
Provides simplified Gherkin style syntax for Mocha tests by extending the functions in the standard 'BDD' interface.
/*global define, Mocha*/
/**
Provides simplified Gherkin style syntax for Mocha tests by extending the functions in the standard 'BDD' interface.
** See {@link http://mochajs.org/#bdd-interface} for Mocha documentation.
** Feature and Scenario act as a wrapper for 'describe'
** Given, When, Then, But & And act as a wrapper for 'it'
** Feature, Scenario, Given, When, Then, But & And functions are capitalised as otherwise some of them would conflict with CoffeeScript keywords.
@module bdd-feature
@author Matt Barker
@MatthewBarker
MatthewBarker / repeat.js
Created March 24, 2015 10:17
Repeat polyfill - taken from MDN for use in JSfiddle
if (!String.prototype.repeat) {
String.prototype.repeat = function(count) {
'use strict';
if (this == null) {
throw new TypeError('can\'t convert ' + this + ' to object');
}
var str = '' + this;
count = +count;
if (count != count) {
count = 0;
@MatthewBarker
MatthewBarker / kendoSparkline.js
Created March 24, 2015 10:20
Knockout-Kendo sparkline custom binding
ko.kendo.bindingFactory.createBinding({
name: 'kendoSparkline',
bindingName: 'kendoSparkline',
watch: {
data: function(value) {
ko.kendo.setDataSource(this, value);
}
}
});
@MatthewBarker
MatthewBarker / kendoChart.js
Created March 24, 2015 10:22
Knockout-Kendo custom chart binding override to watch the graph options
ko.kendo.bindingFactory.createBinding({
name: 'kendoChart',
watch: {
data: function (value) {
ko.kendo.setDataSource(this, value);
},
options: function (value) {
//unwrap observables from the options object
var jsonOptions = ko.toJSON(value);
//convert the unwrapped json to a plain object
@MatthewBarker
MatthewBarker / kendoDataBar.js
Last active August 29, 2015 14:17
Knocout-Kendo custom binding for data bars
ko.bindingHandlers.kendoDataBar = {
init: function (element, valueAccessor,
allBindingsAccessor, viewModel, bindingContext) {
var data = ko.computed(function() {
var field = ko.utils.unwrapObservable(valueAccessor().field),
comparisons = ko.utils.unwrapObservable(valueAccessor().comparisons),
numerator = 0,
max = 0,
sum = 0,
values = [],
@MatthewBarker
MatthewBarker / knockoutKendoNumber.js
Created March 24, 2015 10:29
Knockout-Kendo number custom binding
ko.bindingHandlers.number = {
update: function(element, valueAccessor, allBindingsAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor()),
precision = ko.utils.unwrapObservable(allBindingsAccessor().precision) || ko.bindingHandlers.number.defaultPrecision,
format = '###,###,###,###.' + '0'.repeat(precision) + ';(###,###,###,###.' + '0'.repeat(precision) + ')',
formattedValue = kendo.toString(parseFloat(value, 10), format);
ko.bindingHandlers.text.update(element, function() { return formattedValue; });
},
defaultPrecision: 0
@MatthewBarker
MatthewBarker / kendoBullet.js
Created March 24, 2015 12:25
Knockout-Kendo custom binding for a bullet graph HTML helper
ko.bindingHandlers.kendoBullet = {
init: function (element, valueAccessor,
allBindingsAccessor, viewModel, bindingContext) {
var options = ko.utils.unwrapObservable(valueAccessor());
options.type = 'bullet';
if (!options.chartArea) {
options.chartArea = {};
}