CSC/ECE 517 Fall 2014/ch1b 34 kr: Difference between revisions
Line 55: | Line 55: | ||
We will start with creating a basic Rails application that performs CRUD operations. We shall use PostgreSQL and Rspec here. | We will start with creating a basic Rails application that performs CRUD operations. We shall use PostgreSQL and Rspec here. | ||
Lets call this application: | Lets call this application: [https://www.honeybadger.io/blog/2013/12/11/beginners-guide-to-angular-js-rails/ Restauranteur] | ||
====Initial Setup==== | ====Initial Setup==== |
Revision as of 04:30, 7 October 2014
AngularJS + Rails
Overview
AngularJS is an open source client-side Javascript framework for creating web applications with dynamic web pages and is maintained by Google. AngularJS has extended HTML's syntax and implemented it with Model-View-Controller architecture so that the various components of Javascript can be expressed clearly. It provides functionalities like data binding and dependency injection which helps shorten the crude javascript code. It has a simple object model that supports testability and clean code organization.
Rails is an open source web application framework written in Ruby by David Heinemeier Hansson. It is extremely productive for developing database-backed web applications. It uses Model-View-Controller architecture and requires fewer total lines of code than other frameworks thus making it atleast ten times faster.
Background
AngularJS
AngularJS was developed by Misko Hevery and Adam Abrons at Brat Tech LLC in 2009 as the software behind an oline JSON storage service. Later, it was released as an open source library. The library is being developed and maintained by Hevery with his fellow Google employees.
Rails
Rails was extracted from Basecamp, a project management tool by 37signals but it was released as an open source project in July 2004. It gained fame after Apple decided to ship it with Mac OS X, which was released in October 2007.
Why AngularJS + Rails?
One of the key reasons of using AngularJs is API-Driven Application approach, i.e. separation of User Interface from back-end (Rails in our case). Increasing number of application these days (Rails Application) are building their own APIs which can they interface with mobile applications (like iOS), or other developer applications can communicate with their applications, and in our case to communicate with front-end applications. By following this paradigm, developers gets an opportunity to build a front-end as well as back-end APIs along with your application, it means front-end exposes its methods to the JSON API in the back-end and the front-end API can be clubbed with any back-end and vice-versa without any need for re-configuration.
So the question is why to club AngularJS with Rails and what are its advantages? AngularJS is a front-end framework, what this means is, it is designed to run on the client side, therefore for any real application requiring CRUD operations a back-end is necessary. This is where Rails comes into picture, it provides a back-end to the AngularJS application so that large amount of data can be stored in database. Another issue with using standalone AngularJS application is that it creates Single-page Applications (SPA), i.e. the same page or components on the page gets refreshed, which makes Search Engine Optimization extremely tricky, as a result the possibility of making the web page searchable reduces drastically. Whereas in case of Rails the web pages are rendered by Rails on the server, which makes them available to Google for SEO based search.
Getting Started
Prerequisites
AngularJS
Following are the pre-requisites for learning AngularJS:
- Moderate knowledge of HTML, JavaScript and CSS.
- Basic Model-View-Controller concepts.
- The Document Object Model.
- JavaScript functions, events, and error handling.
Rails
- Comprehensive knowledge of Ruby.
- Moderate knowledge of databases.
- Basic Model-View-Controller concepts.
Step 1: Creating Base Rails app
We will start with creating a basic Rails application that performs CRUD operations. We shall use PostgreSQL and Rspec here.
Lets call this application: Restauranteur
Initial Setup
$ rails new restauranteur --database=postgresql --skip-test-unit
Comparison with other front-end frameworks
The most popular front end frameworks these days include AngularJS, BackboneJS and EmberJS. These can be compared on the basis of the following factors<ref>http://www.airpair.com/js/javascript-framework-comparison</ref>:
Community
Community is the most important factor to consider when choosing a framework as a strong and large community means strong support and help towards the framework. More questions will be answered, more tutorials will be available. In short, larger the community means more the number of resources to exploit.
So, if compared on the basis of community, AngularJS is the clear winner as there are more StackOverflow questions , YouTube tutorials, GitHub Contributors available for Angular then the rest of the frameworks. Also, in the recent years AngularJS has gained more and more popularity compared to the other two.
Framework Size
For a website to be successful, it should have good page load speed as users these days need good speed of browsing. In order to load pages fast, the front end framework should be as small as possible so that they can be loaded faster. If we compare the sizes of the frameworks, BackboneJS (6.5 kb) is the smallest among AngularJS(39.5 kb) and EmberJS(90 kb). But, BackboneJS needs UnderscoreJS(5 kb) and jQuery(32 kb) along with some other third party plugins which makes the size large. Thus, AngularJS is the clear winner among these frameworks.
Templating
Front end frameworks generally use different kind of templates to style their code in a specific way. There are many kinds of templates available that an be used. BackboneJS doesn't provide any templating engine of its own but AngularJS and EmberJS have there own templating system. The difference between the templating engines of the three frameworks can be understood from the following examples :
AngularJS
Angular's templating engine simply extends HTML syntax. It uses binding expressions to bind variables between controllers and models. These binding expressions are enclosed by doubly curly braces. For example:
<ul> <li ng-repeat="recipe in recipes" title="{{recipe.title}}"> {{recipe.description}} </li> </ul>
EmberJS
EmberJS uses Handlebars template engine. Handlebars is an extension of Mustache templating engine. It does not understand the Document Object Model structure so it is not aware of the context in which it is being used. It just does the simple string transformation. Another templating engine is being developed called HTMLBars which works on these flaws of the Handlebars. Since, HTMLBars is not yet ready therefore Handlebars is currently being used. For example:
<ul> {{#each recipes}} <li {{bind-attr title=name}}> {{desc}} </li> {{/each}} </ul>
BackboneJS
BackboneJS doesn't have any templating engine of its own but it can be integrated with many third party templating engines. Since, Underscore is a Backbone dependency so it is used by default for the templating purposes. But, underscore has a very basic templating engine and it has to be used in the mix with javascript. For example:
<ul> <% _.each(recipes, function(recipe) { %> <li title="<%- recipe.name %>"> <%- recipe.desc %> </li> <% }); %> </ul>
Reference Material
Following are the references to learn Ruby on which Rails is based :
Following are the references to learn Rails :
Following are the references to learn AngularJS :
References
<references/>
Name | AngularJS + Rails |
Category | Open Source Software |
Type | Web Application Framework |