Prototype based controller with Angular 1.4

יום שלישי, יוני 30, 2015

I just encountered a brilliant post written by Eyal Vardi (Hebrew only) Eyal talks about how we can use Angular metadata stored inside $inject to automatically inject the controller's dependencies into the controller instance This way you don't need to manually copy local dependency parameters into the controller instance. Consider the following Typescript controller + HTML view class HomeCtrl {     $scope;     $http;     items: any;     constructor($scope, $http) {         this.$scope = $scope;         this.$http = $http;     }     refresh() {         this.$http.get("/api/item").then((items) => {             this.items = items;         });     } } angular.module("MyApp").controller("HomeCtrl", ); <div ng-controller="HomeCtrl as ctrl">     <ul>         <li ng-repeat="item in ctrl.items">             <span>{{}}</span>         </li>     </ul>     <div>         <button ng-click="ctrl.refresh()">Refresh</button>     </div> </div> As you can see all dependencies specified by angular.controller function are copied manually into the controller instance so we can later use them inside the refresh function. When writing "plain" Angular controller based on simple function (without prototype) the dependencies are specified as local parameters...
tags: , ,
no comments

Adding zone.js to Angular 1.4

יום שבת, יוני 27, 2015

(I find below bits quite useful so I add them to github. You can get them by using bower install angular1-zones) One of the cool features of Angular 2 is that there is no need to call $scope.$apply. This is true even if you are using 3rd party asynchronous library which Angular is unaware of. The magic resides inside Angular 2 library named zone.js The library overrides all standard browser APIs that are considered asynchronous. zone.js injects its own implementation and uses it to monitor the start and completion of any asynchronous activity. Consider the following Angular 1.4 source code function HomeCtrl($scope) {     $scope.change = function () {         setTimeout(function () {             $scope.message = "XXX";         }, 1000);     } } This is a plain controller...
tags: , ,
one comment

Managing long running operation using promise and $$q

יום ראשון, אפריל 5, 2015

Through my previous post I described the challenges when implementing long running operation. Basically, you need to break your long running operation into small steps where each step is isolated by setTimeout(0) from the next step. At this post I would like to cover a technique for using promise object to manage the long running operation. Angular's promises offer the common then/catch functions but also offer an additional function named notify The notify function can be used to send notifications to the client about the progress of the running operation. For example, suppose a runTask function is responsible for initiating a long running...
tags: , ,
no comments

Updating DOM during long running operation

יום שבת, מרץ 21, 2015

Suppose I need to implement a long running operation (longer than a few seconds) and during that operation display some user feedback Well, JavaScript is single threaded. This means that during operation execution the single thread is busy and cannot handle UI events like mouse move and mouse click. From end user perspective the UI is dead. Modern solution is to use Web Worker. As long as your algorithm is pure (only accesses JavaScript objects) then you are OK. However, if the algorithm needs to access DOM objects you cannot use web workers since they have no access to the DOM I agree that...

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


יום חמישי, פברואר 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; = name; = email; } Contact.prototype.dump = function () {     console.log( + ", " +; } = 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", ""); 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

Building Single Page Application – Bundle Orderer

יום שישי, דצמבר 27, 2013

In my previous post I discussed a way to arrange scripts in a single page application. Short story: each application layer is associated with exactly one bundle. Assuming a vertical dependency between layers, we can just include the bundles in the right order and any time we add a new file to a layer that file is automatically added in the right position with respect to other files in other layers. For example, below is a common bundle configuration in my single page applications bundles.Add(new ScriptBundle("~/bundles/lib")     .IncludeDirectory("~/Scripts/Lib", "*.js", true)); bundles.Add(new ScriptBundle("~/bundles/server")     .IncludeDirectory("~/Scripts/Server", "*.js", true)); bundles.Add(new ScriptBundle("~/bundles/bl")     .IncludeDirectory("~/Scripts/BL", "*.js", true)); bundles.Add(new ScriptBundle("~/bundles/views")     .IncludeDirectory("~/Scripts/Views", "*.js", true)); bundles.Add(new ScriptBundle("~/bundles/app")     .IncludeDirectory("~/Scripts/App", "*.js", true)); Please note that each bundle is configured to include a specific directory. This means that when you add a...

Building Single Page Application

יום חמישי, אוגוסט 23, 2012

Taking HTML to the extreme is all about developing Single Page Application (SPA). Fetch all required HTML + JavaScript + CSS files from the server and then use the server only for data manipulation. No more “HTML server side rendering” and no more “partial page updates”. Doing so allow you to offer your end users a much more attractive user experience. For example, since all required files are up front loaded to the browser we can use HTML5 Offline Web Application feature to allow our web application to be executed on the client machine even if the server...
no comments