Skip to content

Instantly share code, notes, and snippets.

View menacestudio's full-sized avatar

Dennis Rongo menacestudio

View GitHub Profile

This afternoon I encountered a race condition in an Angular app I'm working on. Essentially my controller was pushing some values to an Array on its scope and something (I wasn't sure what) was asynchronously overriding the Array's contents. The Array was being used by a custom directive- written by someone else- as well as an ngModel and it wasn't clear who was making the change.

I ended up trying something I had not done before and it worked well enough that I thought I'd post it here in case it helped anyone else.

First I enabled The "Async" option in Chrome's "Sources > Call Stack" panel.

Next I set a breakpoint in my controller where I was modifying the Array. When I hit that breakpoint, I ran the following code in my console:

Object.observe(this.theArray, function(changes) {
@menacestudio
menacestudio / slider.ts
Created March 17, 2015 22:47
An AngularJS slider directive
slider.$inject = [];
function slider() {
return <ng.IDirective>{
restrict: 'E',
replace: true,
scope: {
model: '=ngModel',
isRequired: '=',
min: '=',
@menacestudio
menacestudio / numericOnly.ts
Last active August 29, 2015 14:17
An AngularJS decorator directive to only allow numeric values.
numericOnly.$inject = ['$log'];
function numericOnly(
$log: ng.ILogService): ng.IDirective {
return <ng.IDirective>{
restrict: 'A',
scope: true,
link: link
};
@menacestudio
menacestudio / datepickerFormatter.js
Created March 13, 2015 21:39
An AngularJS decorator directive to apply automatic formatting to Angular UI datepickers.
function datepickerPopup() {
return <ng.IDirective>{
restrict: 'EAC',
require: 'ngModel',
link: function ($scope, $element, $attrs, controller) {
controller.$formatters.shift();
}
}
}
angular.module('app').directive('datepickerPopup', datepickerPopup);
@menacestudio
menacestudio / sortableGrid.js
Created March 13, 2015 21:37
An AngularJS directive to add sorting capability to table headers.
sortableGrid.$inject = [];
function sortableGrid() {
return <ng.IDirective>{
restrict: 'A',
scope: {
order: '=',
by: '=',
reverse: '='
},
@menacestudio
menacestudio / scoreDirective.js
Created March 13, 2015 21:35
An AngularJS directive for handling score input.
scoreInput.$inject = [];
function scoreInput() {
return <ng.IDirective>{
restrict: 'E',
replace: true,
scope: {
model: '=ngModel',
isRequired: '=',
isReadonly: '=',
@menacestudio
menacestudio / index.html
Created December 9, 2014 05:20
Angular CRUD
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js@*" data-semver="1.3.5" src="https://code.angularjs.org/1.3.5/angular.js"></script>
<script src="https://code.angularjs.org/1.3.5/angular-route.js" data-semver="1.3.5" data-require="angular-route@*"></script>
<script data-require="angular-resource@1.3.5" data-semver="1.3.5" src="https://code.angularjs.org/1.3.5/angular-resource.js"></script>
<link href="style.css" rel="stylesheet" />
<script src="script.js"></script>
</head>
@menacestudio
menacestudio / service.js
Created December 7, 2014 09:46
Angular ngResource sample
angular.module("exampleApp", []).service('exampleService', ["$http", "$q" ,function ($http, $q) {
var service = {
returnedData: [],
dataLoaded:{},
getData = function(forceRefresh)
{
var deferred = $q.defer();
if(!service.dataLoaded.genericData || forceRefresh)
{
@menacestudio
menacestudio / controller.js
Last active August 29, 2015 14:10
Angular using Angular directives for Bootstrap
(function(app) {
app.controller('Ctrl', Ctrl);
app.controller('CtrlModal', CtrlModal);
//Ctrl.$inject = ['$scope', '$modal', 'dataservice'];
function Ctrl($scope, $modal, dataservice) {
var vm = this;
@menacestudio
menacestudio / sample_cte_paging.sql
Created November 13, 2014 03:19
Sample paging in TSQL using common table expression (CTE)
-- Calculate paging
DECLARE @topRecord VARCHAR(20) = CAST(((@pageSize+@offset)+1) AS VARCHAR)
DECLARE @cte_query NVARCHAR(MAX)
SET @cte_query = WITH cte_res
AS ( SELECT ROW_NUMBER() OVER(ORDER BY ['+@sortBy+'] '+@sortDir+') AS rowid, count(*) over() as TotalRecords,* from (SELECT * FROM #someTable WITH(NOLOCK)) as table1)
select rowID, TotalRecords, id, someField from cte_res with(nolock) where rowid>'+ CAST(@offset AS VARCHAR) +' and rowid< ' + @topRecord
EXECUTE sp_executesql @cte_query