Configuring a Provider

24 בפברואר 2015

What is wrong with below code ? angular.module("MyApp", ).config(function (MyProvider) { }); angular.module("MyApp").provider("My", function () {     this.$get = function () {         var service = {             doSomething: function () {                 console.log("Do something ...");             }         };         return service;     } }); Nothing. But, depends on the version of Angular you are using you might encounter errors. Running above code with Angular 1.2.9 generates the following error: Failed to instantiate module MyApp due to unknown provider: MyProvider Apparently, Angular 1.2.9 queues config callbacks and provider registration requests into the same queue. Lets look at Angular 1.2.9 implementation for a module function module(name, requires, configFn) {         var invokeQueue = ;         var runBlocks = ;         var moduleInstance = {             provider: invokeLater('$provide', 'provider'),             config: invokeLater('$injector', 'invoke'),         };         return  moduleInstance;         function invokeLater(provider, method, im) {             return function() {                 invokeQueue();                 return moduleInstance;             };         }     }); }; A module instance holds two private queues, invokeQueue and runBlocks. Both provider and config methods are using invokeQueue queue Later, Angular loads the module by walking the queues and invoking all registered callbacks. Our problematic sample code first registers a config block angular.module("MyApp", ).config(function (MyProvider) { }); And only...
tags: , ,
no comments

$injector.instantiate

19 בפברואר 2015

Not every class inside Angular application is a service For example, consider the following class definition function Contact($http, name, email) {     this.$http = $http;     this.name = name;     this.email = email; } Contact.prototype.dump = function () {     console.log(this.name + ", " + this.email); } Contact.prototype.save = function () {     //     //  Work against this.$http     // } Above class represents a single contact and therefore is instantiated multiple times during application execution. This means, that we cannot register it as an Angular factory/service However, the Contact class has $http dependency. This means that the caller must supply it manually. For example, assuming the caller is a controller which has a reference to $injector it can do the following angular.module("MyApp").controller("HomeCtrl", function ($injector) {     var $http = $injector.get("$http");     var contact = new Contact($http, "Ori", "ori.calvo@gmail.com"); contact.dump(); }); However, what if the Contact class has many dependencies that must be resolved. Calling $injector.get for every dependency is quite tedious Fortunately, Angular offers a...
tags: , ,
no comments

Mobile Icons

14 בפברואר 2015

You know why I like Twitter's Bootstrap? Because of the Icon gallery I don't know what about you but for me it was always a challenge finding a nice icon gallery that fits my application needs. I used several commercial libraries but even then I found the supplied icon gallery to be quite limited Bootstrap has a nice icon gallery which are vector based (not pixel) which means you can enlarge each image and quality is kept. So, why am I telling you about it? Bootstrap is not new to anyone Well, I just found that they add additional images to their icon...
tags: , ,
no comments

CSS Pixel Insenity

1 בפברואר 2015

There are lots of articles trying to explain what is a pixel from CSS perspective. Here are the most popular ones A pixel is not a pixel is not a pixel A Pixel Identity Crisis Probably the most important knowledge base is the formal W3C document CSS Values and Units Module Level 3 If you read those articles and are still struggling to understand the true meaning of CSS pixel than keep reading. Instead of just tell you the formal definition lets take a practical path to defining a CSS pixel iPhone 3GS has a screen resolution of 320 x 480 iPhone 4 has a screen resolution...
no comments