Skip to content

Instantly share code, notes, and snippets.

@toddmotto
toddmotto / mock-data.txt
Created April 20, 2016 11:28
Excel Online / Kendo Spreadsheet data
OrderDate
Region
Rep
Item
Units
@toddmotto
toddmotto / annotate.js
Created July 16, 2014 21:26
Angular's annotation mapping process and performance
/**
* Read the post for full details, hope these comments are helpful!
* http://toddmotto.com/angular-js-dependency-injection-annotation-process
*/
function annotate(fn, strictDi, name) {
var $inject,
fnText,
argDecl,
last;
@toddmotto
toddmotto / angular.annotate.js
Created July 16, 2014 21:05
Angular's annotation function
function annotate(fn, strictDi, name) {
var $inject,
fnText,
argDecl,
last;
if (typeof fn === 'function') {
if (!($inject = fn.$inject)) {
$inject = [];
if (fn.length) {
@toddmotto
toddmotto / extend.js
Created June 30, 2014 12:42
Even smaller Object extend() method, with no reference cloning: http://jsfiddle.net/LR639
function extend (target, source) {
var a = Object.create(target);
Object.keys(source).map(function (prop) {
prop in a && (a[prop] = source[prop]);
});
return a;
};
@toddmotto
toddmotto / extend.js
Last active August 29, 2015 14:03
ECMAScript 5 extend(), preserves original Object values
function extend (target, source) {
target = JSON.parse(JSON.stringify(target));
Object.keys(source).map(function (prop) {
Object.prototype.hasOwnProperty.call(target, prop) && (target[prop] = source[prop]);
});
return target;
};
@toddmotto
toddmotto / listeners.js
Last active September 14, 2016 04:59
Automatic unbinding on $scope.$destroy for $rootScope listeners
/*!
* $rootScope listeners, remember to unbind on $destroy
*/
var $rootListeners = {
'transmitProgress': $rootScope.$on('transmit:progress', transmitProgress),
'transmitSuccess': $rootScope.$on('transmit:success', transmitSuccess),
'transmitError': $rootScope.$on('transmit:error', transmitError)
};
// iterate the Object and pass the methods to be called on $destroy
@toddmotto
toddmotto / xhr.js
Created June 10, 2014 13:56
XHR wrapper for AngularJS $http
/**
* @name xhr
* @desc Dynamic $http/$q
* @param {String} [type] HTTP method
* @param {Array} config Array of config to be called with .apply()
* @private
* @returns {Object} deferred.promise
*/
var xhr = function (type, config) {
if (!config && angular.isArray(type)) {
@toddmotto
toddmotto / q-http.js
Created May 28, 2014 09:34
Using $q.defer() with $http in Angular to return the Promise Object only
var query = function (endpoint) {
var deferred = $q.defer();
$http.get(endpoint)
.success(function (data) {
deferred.resolve(data);
})
.error(function (data) {
deferred.reject(data);
});
return deferred.promise;
@toddmotto
toddmotto / example.md
Created May 27, 2014 13:11
Dynamically change $location (with query params) in Angular using search() and path()

Input:

var someDynamicParam = 9812894732432;
$location.search('myParams', someDynamicParam).path('/request');

Output:

@toddmotto
toddmotto / gist:ce1023cb755b31210563
Created May 6, 2014 14:53
Debunking the British Gas paste event
var input = document.querySelector('input[type=password]');
var britishGasPasteHandler = function () {
if (window.certificate) {
certificate.revoke();
}
};
input.addEventListener('paste', britishGasPasteHandler, false);