Tuesday, March 31, 2015

Useful Git Command

1. Squash
Say your bug fix branch is called bugfix and you want to merge it into master:
git checkout master
git merge --squash bugfix
git commit

2. Delete unexpected stash pop
git clean -f

3. Delete remote branch (the local one has been deleted)
git push origin --delete 

4. Reset commit
git reset --hard <HEAD>
git push -f

5. How to show or change Git username
http://alvinalexander.com/git/git-show-change-username-email-address

6. Generating SSH keys
https://help.github.com/articles/generating-ssh-keys/

7. Check who is the latest person updated the branch
git for-each-ref --format='%(refname) - %(authorname) (%(committerdate))' 'refs/remotes'

8. Prune the local list of remote branches
git remote prune origin

Sunday, March 29, 2015

Unexpected request GET error in Angular JS

I got the
Error: Unexpected request: GET components/template.html
No more request expected
at $httpBackend ... 

For me, the reason for this problem is:
The relative path of template.html in karma.conf.js preprocessors, beforeEach(module) and myDirective.js are different, i.e:
in the karma.conf.js, it was:
preprocessors: {
    'src/components/template.html': ['ng-html2js']
},

in the beforeEach(module), it was:

beforeEach(module('src/components/template.html'))

and in the myDirective,js, it was:

function myDirective(){
    return{
        restrict: 'EA',
        templateUrl: 'src/components/template.html'
    };
}

To solve the problem, I change all the relative path to
'src/components/template.html'

Wednesday, March 25, 2015

Good articles about AngularJS


Basic usage
1. AngularJS code style guide
- Jhon Papa: 10 AngularJS patterns
https://www.youtube.com/watch?v=UlvCbnKAH3g
https://github.com/johnpapa/ng-demos
- Todd Motto
https://github.com/toddmotto/angularjs-styleguide
- Angular Best Practices: Directory Structure
https://scotch.io/tutorials/angularjs-best-practices-directory-structure
- Scalable code organization in AngularJS
https://medium.com/opinionated-angularjs/scalable-code-organization-in-angularjs-9f01b594bf06
- Code Organization in large AngularJS and JavaScript Applications
http://cliffmeyers.com/blog/2013/4/21/code-organization-angularjs-javascript
2. Understanding $emit, $broadcast and $on in AngularJS
http://www.dotnet-tricks.com/Tutorial/angularjs/HM0L291214-Understanding-$emit,-$broadcast-and-$on-in-AngularJS.html
3. Recipes with Angular.js
http://fdietz.github.io/recipes-with-angular-js//controllers/testing-controllers.html
4. Angularjs directive
http://www.chroder.com/2014/02/01/using-ngmodelcontroller-with-custom-directives/
5. Controller in directive
http://www.bennadel.com/blog/2446-using-controllers-in-directives-in-angularjs.htm
6. Derictive Isolate Scope
http://weblogs.asp.net/dwahlin/creating-custom-angularjs-directives-part-2-isolate-scope
7. Understanding Dependency Injection
https://github.com/angular/angular.js/wiki/Understanding-Dependency-Injection
8. Directive's lifecycle in AngularJS
http://filimanjaro.com/2014/angular-directive-lifecycle-vs-link-compile-controller/
9. Developer Guide Recipe: Provider, Factory, Service, Constant
https://docs.angularjs.org/guide/providers
http://www.learn-angular.org/#!/lessons/the-provider-recipe


AngularJS Unit Test:
1. Introduction to Unit Testing in AngularJS App
http://www.smashingmagazine.com/2014/10/07/introduction-to-unit-testing-in-angularjs/
2. Mocking Dependencies in AngularJS Tests
http://www.sitepoint.com/mocking-dependencies-angularjs-tests/
3. spyOn and createSpy
http://www.htmlgoodies.com/html5/javascript/spy-on-javascript-methods-using-the-jasmine-testing-framework.html#fbid=e4VtfL1YKyJ
4. Unit Testing w/ AngularJS
http://moduscreate.com/unit-testing-w-angularjs/
5. How to Unit Test Controllers
http://nathanleclaire.com/blog/2013/12/13/how-to-unit-test-controllers-in-angularjs-without-setting-your-hair-on-fire/
6. AngularJS Unit Testing Best Practices
http://blog.codeship.com/angularjs-tdd/
7. Introduction to Unit Test: Directives
http://angular-tips.com/blog/2014/06/introduction-to-unit-test-directives/
8. Video: Testing Directives
https://www.youtube.com/watch?v=rB5b67Cg6bc
9. Video: Unit Testing a Directive  ---- Egghead
https://www.youtube.com/watch?v=NV5Zkthh7nw
10. Testing AngularJS directive: handling external templates
http://www.portlandwebworks.com/blog/testing-angularjs-directives-handling-external-templates
11. Unit Testing AngularJS Directives With External Templates
http://www.bluesphereinc.com/blog/unit-testing-angularjs-directives
12. Testing AngularJS directives with External Templates --- using templatecache
http://www.portlandwebworks.com/blog/testing-angularjs-directives-handling-external-templates
13. Testing AngularJS directive templates with Jasmine and Karma
http://www.portlandwebworks.com/blog/testing-angularjs-directives-handling-external-templates
14. Testing directive that appends template to element ---- stackoverflow
http://stackoverflow.com/questions/28363679/testing-directive-that-appends-template-to-element
15. When template path in templateUrl of directive.js, module(<template path>) of directive.spec.js and preprocessor of karma.conf.js are different:
https://github.com/vojtajina/ng-directive-testing/issues/3#issuecomment-87720458
http://tylerhenkel.com/how-to-test-directives-that-use-templateurl/
16. Testing AngularJS Directive with Jasmine ---- From simple to complex
http://busypeoples.github.io/post/testing-angularjs-directives/
http://busypeoples.github.io/post/testing-angularjs-hierarchical-directives/
17. Video and sample code: What, How, When to test ---- e2e, unit test, TFD
https://www.youtube.com/watch?v=9-kumGACGYk
https://github.com/auser/testing-example
18. Angular Testing Cheat Sheet
https://github.com/sydcanem/angularjs-testing-cheat-sheet
19. Change constant value that gets passed to Config for Unit Test
http://stackoverflow.com/questions/25295445/angularjs-change-constant-value-that-gets-passed-to-config-for-unit-test
20. Unit Test custom provider
http://stackoverflow.com/questions/14771810/how-to-test-angularjs-custom-provider
https://github.com/angular/angular.js/issues/2274
21. Example of testing isolate scope
http://plnkr.co/edit/QXprEUof2Ps0Vivg4L34?p=preview
http://stackoverflow.com/questions/27697747/angular-testing-directive-with-templateurl-isolate-scope
22. Introduction to Unit Test: Services
http://angular-tips.com/blog/2014/06/introduction-to-unit-test-services/
23. Writing Unit Tests for an AngularJS Service
https://busypeoples.github.io/post/writing-unit-tests-for-service-in-angular-js/
24. Supplying Mocks for Services via $provide
http://tech.pro/tutorial/1517/supplying-mocks-for-services-via-provide
25. AngularJS Unit Testing Best Practices
http://blog.codeship.com/angularjs-tdd/
26. Understanding Dependency Injection

Friday, March 20, 2015

close in ModalService --- AngularJS

The close of Modal is in ModalService.

beforeEach(inject(function(_ModalService_){
    close = ModalService.close;
}))

Monday, March 16, 2015

Notes of SCOPE & CLOSURE JS

1. If an RHS look-up fails to ever find a variable, anywhere in the nested scopes, this results in a ReferenceError being thrown by the engine. I's important to note that the error is of the type ReferenceError.
    If the engine is performing an LHS look-up and it arrives at the top floor (global scope) without finding it, if the program is not running in "strict mode", then the global scope will create a new variable of that name in the global scope, and hand it back to Engine.

2. Cheating Lexical
    1) eval
    2) with: Even though a with block treats an object like a lexical scope, a normal var declaration inside that with block will not be scoped to that with block, but instead the containing function scope.

Wednesday, March 11, 2015

Notes: John Papa - 10 AngularJS Patterns (Codes on the Beach 2014)

BE CONSISTENT!

1. Structure Patterns
    1) By Type:
        app/
              controllers/
              services/
              views/
              factories/
              directives/
      2)By Feature
         app/
               dashboard/
               layout/
               people/
               services/
               Speakers/
               Tracks/
               Sessions
       3) Somewhere in the middle
    LIFT
2. Data Patterns
    Separation of Concerns
    Don't put data access in the Controller
3. Decorate the $exceptionHandler Service
4.  Modularity
    Each module can be app, service or widgets
    Modules are containers
    Defining a Module:

    set a module
var app = angular.module('myApp',[]);

    get a module
var app = angular.module('myApp');
 
    inject dependencies into a Module (like a wrapper ):
angular.module('app',[
   'app.avengers',
   'app.dashboard',
   'app.widgets',
   'app.layout'
]);

    Categories of Dependencies:
angular.module('app',[
   //Angular Modules
   'ngRoute',
   //Custome Modules
   'common',
   // 3rd Party Modules
   'ui.bootstrap',
   'breeze.angular'
]);

                        avengers                ngAnimate
ModularApp   dashboard    core    ngRoute
                        widgets                  common
                        layout                     ui-bootstrap

5. Controller pattern
    Controller is like a constructor

angular
    .module('app.avengers')
    .controller('Avenger',Avenger);

// "Classic" $scope syntax
function Avenger($scope){
    $scope.name = 'Captain America';
    $scope.prop = 'shield';
}

// "Controller As" syntax
function Avenger(){
    var vm = this;
    vm.name = 'Captain America';
    vm.prop = 'shield';
}
6. Clean Code
    IIFE(Immediately Invoked Function Expression)
7. Dependency Injection
    Safely Minify Dependencies
 
angular
    .module('app.dashboard')
    .controller('Dashboard',Dashboard);

function Dashboard(common, dataservice){
    // when minified, the common and dataservice may become a and b, which won't be found
}

Safe way 1: Inline Dependency Array
angular
    .module('app.dashboard')
    .controller('Dashboard',['common','dataservice',Dashboard]);
function Dashboard(common, dataservice){
}

Safe way 2: $inject
angular
    .module('app.dashboard')
    .controller('Dashboard',Dashboard);
Dashboard.$inject = ['$q','dataservice','logger'];
function Dashboard($q, dataservice, logger){
}

8. Tasks and Annotations
    Grunt: Configuration over code; File based
    Gulp: Code over configuration; Stream based
9. Breeze, Local Storage, Validation, Karma, Gulp and More

Saturday, March 7, 2015

Problem when assigning value to a variable defined outside of a function

I wrote this code:

searchBtnIsEnabled: {value: function(){
    var isEnabled;
    this.searchBtn.isEnabled().then(function(result){
      isEnabled = result;
      console.log("the isEnabled is: "+isEnabled);
    });
    console.log(isEnabled);
  }}


But the logged isEnabled is undefined. Then I change it to:

searchBtnIsEnabled: {value: function(callback){
    this.searchBtn.isEnabled().then(function(result){
      callback(result);
    })
  }}

this.searchBtnIsEnabled(function(enable){
    console.log("The enable is: "+enable);
})

Then it works.

REASON : Javascript Asynchronous

Please read:
http://tech.pro/blog/1402/five-patterns-to-help-you-tame-asynchronous-javascript
http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron