Showing posts with label AngularJs. Show all posts
Showing posts with label AngularJs. Show all posts

Make javascript execution synchronus - A Scenario in Angularjs

Make Javascript Execution Synchronous - A Scenario in Angularjs

Last week I came to deal with a problem in my angularjs application. I want to execute a code block only after getting the response from rest service call. This code block is not inside success/failure call back function, but it is below service call code as below.

function function1() {
//rest call
dataService.serviceCall().then(function (data) {
            console.log("label a")
            }, function (error) {
                console.log("call failed"+ error);
                
            });
            
  console.log("label b")
 }

I want to print "label b" only after printing "label a". But now, as javascript is asynchronous, 'label b' is first printed then 'label a'. It is not waiting for the response of the service call.

To print "label b" only after printing "label a", I found two solutions,
1.  Put the label a code inside callback function.
eg :  
function1() {
//rest call
dataService.serviceCall().then(function (data) {

                 console.log("label a");
                printB();
                
            }, function (error) {
                 console.log("call failed"+ error);
             });
            var printB = function(){
                console.log("label b");
            }
        }
In real case, the code block was not a single line code, that's why /i put it outside service call. If it is a small one i could have been included in success callback function it self. Here I moved all the code into a new function in side function itself, so that I will get the all local references. Then I called function inside success callback function. 

I added a new function syncCall. This will call function1. Js promises or deffered is used to make it synchronous. If response is received successfully, 'label a' is printed, promise is resolved and returned. On successful resolution of promise, 'label b' is printed.
eg: 

syncCall();

        function syncCall() {
            var deferred = $q.defer();
            function1(deferred).then(function () {
                console.log("label b");
            }, function () {
                console.log("err");
            });
        }

        /* Filter items */
        function function1(deferred) {
dataService.serviceCall().then(function (data) {

                 console.log("label a");
                deferred.resolve();
                
            }, function (error) {
                 console.log("call failed"+ error);
                deferred.reject();
             });
      return deferred.promise;
        }


AngularJS Prevent Event Propagation Click Example

AngularJS Prevent Event Propagation Click Example

Today I was stuck with a problem in angularjs. I have a table row and a button in table cell. I have bound event event for both table row and button. So I have to disable event on table row click when I click on button, in other words I have to stop propagation of event on button click.

I found a solution as below,

<a href="#" ng-click="functionName(); $event.preventDefault(); $event.stopPropagation();">


$event.stopPropagation(); alone was not working. When I used $event.stopPropagation(); alone, on click it was redirecting to some other page. When I tried adding  $event.preventDefault(); $event.stopPropagation();. It worked.

Reference:
http://stackoverflow.com/questions/10931315/how-to-preventdefault-on-anchor-tags

Yeoman - Create an AngularJs Scaffold

Yeoman - Create an AngularJs Scaffold


Introduction
Yeoman is used to generate a basic scaffolding of app. This will help to start a new project quickly, creating folder structure and configuration, adding necessary libraries/tools like bootstrap, sass etc. and prescribing best practices. There are generators associated with yo for different languages like angular, backbone, etc.

Environment setup and Installation
1. Pre-requisites:

  • Node.js v0.10.x+
  • npm (which comes bundled with Node) v2.1.0+
  • git

2.Install yo, bower and grunt
   C:\>npm install --global yo bower grunt-cli

3. Install generator-angular and generator-karma using this command:
   C:\>npm install --global generator-angular@0.11.1 generator-karma

Generate Angular App
Create a folder for app,
  C:\>md yApp
  C:\>cd yApp

Now access generators via the Yeoman menu,
  C: \yApp>yo


Select Angular as generator.  On selecting angular, it will ask whether to include different components like Sass, bootstrap and different angular-modules.


On hitting enter key, different modules will be installed and app will be generated. We can edit this app and develop our app. Like we can add new controller in controller folder, add new view in views folder. Also add other libraries using bower.


Run App: Start Server
Use below command to run grunt task to start Node-based http server on localhost:9000.
C:\yApp>grunt serve


Run Unit Tests-Using Karma and Jasmine
The Angular generator has included two test frameworks: ngScenario and Jasmine. A test directory is created in the root folder, creates test spec files, created a karma.conf.js file, and pulled in the Node modules for Karma while generation of app. Add/Edit spec file for testing different modules. Use below command to run unit tests.
C:\yApp>grunt test

Get production app: Minification and Uglification
Concatenate and minify our scripts and styles to save on those network requests, run unit tests, optimize images if we were using any, etc. are one to make the code production ready using the command below,
C:\yApp>grunt

AngularJS Sort and Search in Table

AngularJS Sort and Search in Table

Today I have implemented sort and search functionality for table, just like jquery datatable, referring a tutorial on scotch.io. It is easy to implement.

https://scotch.io/tutorials/sort-and-filter-a-table-using-angular

Create HTML5 App using SenchaCmd - Sencha ExtJs 5

Create HTML5 app using cmd commands
Sencha Ext Js 5 

We can create HTML5 apps using Sencha cmd just typing some commands. See below.

sencha -sdk /path/to/sdk generate app MyApp /path/to/myapp
cd /path/to/myapp
sencha app watch 

eg: Open command prompt and enter as below,


If we go to http://localhost:1841/,



If we check the folder,


Any Error? Check following are installed on your machine. (Windows)

1. Java Runtime Environment version 1.7
How to check? Enter java -version in cmd
Installed, still not coming? check java is included in path environment variable
eg: C:\Program Files (x86)\Java\jre7\bin;

2. Ruby
How to check? Enter ruby -v in cmd
Installed, still not coming? check ruby is included in path environment variable
eg: C:\Ruby21\bin;

3. Sencha Cmd
How to check? Enter sencha in cmd
Installed, still not coming? check sencha cmd is included in path environment variable
eg: C:\Users\username\bin\Sencha\Cmd\5.1.1.39