Sunday, August 26, 2012

Angular.js for ajax CRUD

Its been a busy weekend, not in terms of coding though.  Just did some quick coding now.  This is now a book-markable ajax CRUD talking to conventional REST/JSON backend.


angular.module('ratewatcher', ['ngResource']).
 config(function($routeProvider) {
   when('/lenders', {controller:LenderListController, templateUrl:'lender-list.html'}).
   when('/lenders/new', {controller:LenderCreateController, templateUrl:'lender-detail.html'}).
   when('/lenders/:id', {controller:LenderEditController, templateUrl:'lender-detail.html'}).
 factory('Lender', function($resource) {
  return $resource('/lenders/:id', {id: '@id'}, {});


function LenderListController($scope, Lender) {
  $scope.lenders = Lender.query();
  $scope.remove = function(lender) {
function LenderCreateController($scope, Lender) {
  $ = function() {
    var lender = new Lender({id: 0,
     name: $, 
     category: $scope.lender.category, 
     url: $scope.lender.url

function LenderEditController($scope, $location, $routeParams, Lender) {
 $scope.lender = Lender.get({id: $});
  $ = function() {

I thought the scala backend was really concise, but it turns you can also make the front end javascript just as concise.  Coupled with bootstrap css and basic html, this seems pretty good.

Suggested readings: - Wire Up a Backend example$resource$route

Thursday, August 23, 2012

Angular.js my first few mins impression

I have been looking at Javascript frameworks.  I have experience in GWT and lately trying to learn ember.js.  So far ember.js is really powerful, but the docs is not that great.  Ember.js has made a lot of changes and the only way to learn it seems to be to read the code/api.  Since my javascript is not that good, it was taking me a bit of time to learn the magic of ember.js.  This lead to me look again a bit, I found angular.js because it was in google's CDN api.

Having read the watch and read the front page of angular.js it seems to be just as powerful as ember.js.  Its got bi-directional binding, a template engine, name spaces.  The terminology they have is FP (ala Scala).  They have apply, future promise.  So gave a try.

function LenderController($scope, $http) {
 $scope.lenders = [];
  $scope.all = function() {
    $http.get('/lenders').success(function(data) {
      $scope.lenders = data;

<div class="span10" ng-controller="LenderController">
    <table class="table striped">
        <tr ng-repeat="lender in lenders"> 
            <td>{{}}</td> <td>{{lender.category}}</td> <td>{{lender.url}}</td> 
    <a href="" ng-click="all()">fetch all</a>
Pretty short and direct code for fetching from json/rest backend and displaying it on the view.  I think Angular.js is powerful and yet easy for a newbie to pickup.  That is my first few mins impression of it.