Thursday, October 9, 2014

AngularJS Get Its POJO Back

Earlier AngularJS version (e.g., 0.9) has no $scope:

function GreetingCtrl(){
    this.greeting = 'Hello';
    this.name = 'Michael';
}    

<div ng:controller='GreetingCtrl'>
      <p>{{greeting}} {{name}}</p>
</div>    

Live example: http://jsfiddle.net/ywuwwgu5/


That's one of the things I find elegant when AngularJS was in its infancy. Aside from there's no special class to inherit from, there is no specialized object(e.g., $scope) the framework need to use in order to do model-binding, just a POJO would bind fine

So on later version of AngularJS when $scope was introduced, it felt like, 'oh, my javascript code would be tied to AngularJS!' I'm thinking why the newer AngularJS version can't do the good old magic where the older framework can bind directly to object's properties, to a POJO


Though porting the code to other javascript MV* frameworks it is not the main overriding concern developers has to face when deciding to use AngularJS or not, there's still a value in making a framework able to work with generic code as much as possible. If there's another javascirpt MV* framework that can bind directly to a POJO like the code above, we can just plug that framework to our generic javascript code and we don't have to rewrite a single line of code.

Code portability feels empowering, just like it feel empowering that your SQL knowledge(or at least a subset of) on one RDBMS is transferable to another RDBMS with little to no rewrite. Unlike with using specialized object like $scope, it feels $scope has many moving parts in it and code portability is just a pipe dream, your code is forever tied to AngularJS; nothing wrong with a code associated to high-quality framework like AngularJS, but still



Now that there is Controller as syntax, we could make our javascript code as generic as possible(and portable), again, yay!

With exactly the same generic AngularJS 0.9 controller code above, we can now re-use it on AngularJS 1.2 using Controller as syntax. We just have to adjust our view, we need to prefix the controller's alias on models we wanted to bind to. Neat!
<div ng-controller='GreetingCtrl as g'>
    <p>{{g.greeting}} {{g.name}}</p>
</div>    


Live code: http://jsfiddle.net/x8mwtox9/


A nice insight so AngularJS 1.2's Controller as won't feel too magical, in AngularJS 1.1 we can also use the this approach of AngularJS 1.2 controller above (albeit without the Controller as syntax) and it would still work. AngularJS 1.1 has no Controller as, but we can achieve the AngularJS 1.2 this code and view just by assigning this on a variable in $scope object:
function GreetingCtrl($scope){
    $scope.g = this;
    this.greeting = 'Hello';
    this.name = 'Michael';
}    

<div ng-controller='GreetingCtrl'>
   <p>{{g.greeting}} {{g.name}}</p>
</div>       

Live code: http://jsfiddle.net/hqfhutLo/


With AngularJS 1.2's Controller as feature, aside from it removes the need of assigning this to $scope manually, we don't have to use specialized object such as $scope anymore. Very generic code, neat! :-)



Happy Coding!

No comments:

Post a Comment