Google’s ReCaptcha not appearing?

Recently I had a weird problem that Recaptcha has not rendered at all in most of the cases. It seemed random however, but most of the cases it failed. Later I’ve figured out, that it was caused by some network race conditions. The solution was then quite straightforwad: manual initialization of ReCaptcha after the script has been loaded. I’ve wrapped the whole solution as Angular directive, so everything is in one place. Feel free to use it .directive(‘recaptcha’, [‘$window’, function ($window) { // // The actual directive definition object // return { restrict: ‘AE’, scope: { sitekey: ‘=’ }, template: ‘<script …

Continue Reading

How to call any function as a $filter in AngularJs

Recently I had this little trouble. I had a simple angular controller, which filtered some array of items. I did not want to define the filter in a “global space” because I knew it will be used only by this one controller. So this first thing was to define the custom filter function. Like this $scope.customFilter = (item)=> { return item.someProperty == $scope.filterValue; }; Then call in UI like this. <htmlElement ng-repeat=”item in itemList | filter:customFilter”></htmlElement> So far nothing special, just what is in the angular documentation. But I needed to call that custom filter again, but not from UI …

Continue Reading

How to unit test Angular’s $resource and $http

Unit testing of Angular’s underlying $http can be very tricky. I’ve some spent considerable time to get the unit testing up and running. What was running OK in a live application, was not working at all in a unit test. I was getting error message “No pending request to flush !” all the time. My controller looked something like this //TypeScript Module.controller(“UsersCtrl”, [<any> “$scope”, “$location”, “User”, function ( $scope, $location : ng.ILocationService, User: ng.resource.IResourceClass) { $scope.findUsers = () => { User.query({ email: $scope.searchTerm, showAdHoc: $scope.showAdHoc }, function (data: any) { //data arrived. Do something with them }, function (errorData: any) …

Continue Reading

Jak jsem si zamiloval TDD

Jako asi každý vývojář, jsem i já četl o tom, jak je důležité psát unit testy. Občas jsem nějaké psal, ale nebylo to dostatečné. Na posledním projektu, kde vývoj trval zhruba rok a půl, jsem začal unit testy psát až během posledních několika měsíců. Což bylo sice ostudně pozdě, ale lepší než nic. (Jednalo se o testy na serverové straně, na klientský JS jsme žádné testy neměli. Člověk si musel všechno ručně proklikat. Hrůza.) Takže poučen z předchozích nezdarů jsem si řekl, že v novém projektu musím mít testy pokud možno na všechno. Vyvíjím teď klientskou část aplikace v Angularu. …

Continue Reading

Function string.format ported from .NET to AngularJS

Have your ever wondered if there something like string.format() method as known in .NET but available in Javascript? And waht about AngularJS? The answer is yes. Guys from Microsoft wrote library Ajax Control Toolkit, containing little this gem String.js (I found it via Stackoverflow). I needed it as a filter in AngularJS, so I have ported it there. The code is available on Github. Example of use <h4 class=”result”> {{‘put values here “{0}” and here “{1}”‘ | stringFormat:[‘1’, ‘2’] }} </h4> Result is put values here “1” and here “2” ONLINE DEMO ON JsFiddle Comments welcome.

Continue Reading