Thursday, April 11, 2013

AngularJS controller

So I spent quite a lot of time figuring out how to set up an AngularJS controller without polluting the default namespace/scope. Here is the recipe with a simple click listener showing that it works:

index.html

<doctype html>
<html ng-app="main">
  <head></head>
  <body ng-controller="mainController">
    <button ng-click="log()">Log</button>
    <script type="text/javascript" src="angular.min.js"></script>
    <script type="text/javascript" src="main.js"></script>
  </body>
</html>

main.js

var main = angular.module('main', []);

main.controller('mainController', function($scope) {
  $scope.log = function() {
    console.log('Logging a simple message...');
  }
});

You can easily see which names map to which implementation, e.g. ng-app="main" is angular.module('main', []);, so on and so fourth.

Note: It is extremely important to invoke angular.module with the second argument (in this case, the empty array), otherwise, it is not going to work.

Wednesday, April 3, 2013

Git push to Amazon EC2

If you have an Amazon EC2 instance, it is fairly straightforward to use Git to push your changes to it. The steps to do so are listed below.

On the server

1. Create the directory which will be the project root directory
mkdir myproject

2. Step into the directory and initialize a bare repository
cd myproject
git init --bare

3. Create post-receive and make it executable. This will ensure everything that is pushed is automatically incorporated into the project folder you created.
touch hooks/post-receive
chmod +x hooks/post-receive

4. Edit hooks/post-receive to include the Git work tree where everything that is pushed is eventually put.
vim hooks/post-receive # ...And add the below statement to it
GIT_WORK_TREE=/home/ec2-user/myproject git checkout -f

If you've carefully followed every step, this is all you need to do on the server.

On the client

1. Inside your Git project, add the address of the server
git remote add ec2 ec2-user@your-amazon-address:myproject

2. Finally, you should be able to push to your Amazon EC2 server
git push ec2 master

If you've carefully followed every step, this is all you need to do on the client.

Now you can check that everything you pushed is there in your project folder on the server.

Thanks for reading! Let me know in the comments below if you are having problems with any part of this tutorial.