CSC/ECE 517 Fall 2014/ch1b 34 kr: Difference between revisions
(39 intermediate revisions by 2 users not shown) | |||
Line 3: | Line 3: | ||
==Overview== | ==Overview== | ||
[http://angularjs.org// AngularJS] is an [http://en.wikipedia.org/wiki/Open_source open source] client-side [http://en.wikipedia.org/wiki/JavaScript Javascript] framework for creating web applications with [http://en.wikipedia.org/wiki/Dynamic_web_page dynamic web pages] and is maintained by [http://www.google.com/ Google]. AngularJS has extended [http://en.wikipedia.org/wiki/HTML HTML's] syntax and implemented it with [http://mrbool.com/mvc-in-angularjs/28962 Model-View-Controller] architecture so that the various components of Javascript can be expressed clearly. It provides functionalities like [http://docs.angularjs.org/guide/databinding data binding] and [http://docs.angularjs.org/guide/di dependency injection] which helps shorten the crude javascript code. It has a simple [http://en.wikipedia.org/wiki/Object_model object model] that supports testability and clean code organization. | [http://angularjs.org// AngularJS] is an [http://en.wikipedia.org/wiki/Open_source open source] client-side [http://en.wikipedia.org/wiki/JavaScript Javascript] framework for creating web applications with [http://en.wikipedia.org/wiki/Dynamic_web_page dynamic web pages] and is maintained by [http://www.google.com/ Google]. AngularJS has extended [http://en.wikipedia.org/wiki/HTML HTML's] syntax and implemented it with [http://mrbool.com/mvc-in-angularjs/28962 Model-View-Controller] architecture so that the various components of Javascript can be expressed clearly. It provides functionalities like [http://docs.angularjs.org/guide/databinding data binding] and [http://docs.angularjs.org/guide/di dependency injection] which helps shorten the crude javascript code. It has a simple [http://en.wikipedia.org/wiki/Object_model object model] that supports testability and clean code organization.<ref>http://code.tutsplus.com/tutorials/5-awesome-angularjs-features--net-25651</ref> | ||
[http://rubyonrails.org/ Rails] is an open source web application framework written in [http://www.ruby-lang.org/en/ 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. | [http://rubyonrails.org/ Rails] is an open source web application framework written in [http://www.ruby-lang.org/en/ 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. | ||
Line 30: | Line 30: | ||
==Why AngularJS + Rails?== | ==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. | 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.<ref>https://www.codeschool.com/courses/shaping-up-with-angular-js</ref> | ||
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 [http://en.wikipedia.org/wiki/Search_engine_optimization 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. | 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 [http://en.wikipedia.org/wiki/Search_engine_optimization 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.<ref>http://joelhooks.com/blog/2013/09/15/why-i-built-an-angularjs-training-site-on-rails</ref> | ||
==Getting Started== | ==Getting Started<ref>http://sebastien.saunier.me/blog/2014/02/04/angular--rails-with-no-fuss.html</ref>== | ||
=== | ===Prerequisites=== | ||
=====AngularJS===== | |||
Following are the pre-requisites for learning AngularJS:<ref>http://angular-rails.com/</ref> | |||
# Moderate knowledge of HTML, JavaScript and CSS. | |||
# Basic Model-View-Controller concepts. | |||
# The [http://en.wikipedia.org/wiki/Document_Object_Model Document Object Model]. | |||
# JavaScript functions, events, and error handling. | |||
=====Rails===== | |||
# Comprehensive knowledge of Ruby. | |||
# Moderate knowledge of databases. | |||
# Basic Model-View-Controller concepts. | |||
===Download AngularJS=== | |||
Download AngularJS from [https://angularjs.org/ AngularJS] website. We need to download file: angular-mocks.js | |||
===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 <ref>http://www.honeybadger.io/blog/2013/12/11/beginners-guide-to-angular-js-rails</ref> | |||
====Initial Setup==== | |||
To create project: | |||
<pre> | |||
$ rails new restauranteur --database=postgresql --skip-test-unit | |||
</pre> | |||
To create PostgresSQL user: | |||
<pre> | |||
$ createuser -P -s -e restauranteur | |||
</pre> | |||
Add Rspec to your Gemfile: | |||
<pre> | |||
gem "rspec-rails", "~> 2.14.0" | |||
</pre> | |||
Install Rspec | |||
<pre> | |||
$ bundle install | |||
$ rails g rspec:install | |||
</pre> | |||
Create the database: | |||
<pre> | |||
$ rake db:create | |||
</pre> | |||
===Step 2: Creating the Restaurant model=== | |||
Our next step is to create a model class in Rails | |||
<pre> | |||
$ rails generate scaffold restaurant name:string | |||
</pre> | |||
Make sure that Restaurant Names are unique: | |||
<pre> | |||
# db/migrate/[timestamp]_create_restaurants.rb | |||
class CreateRestaurants < ActiveRecord::Migration | |||
def change | |||
create_table :restaurants do |t| | |||
t.string :name | |||
t.timestamps | |||
end | |||
# Add the following line | |||
add_index :restaurants, :name, unique: true | |||
end | |||
end | |||
</pre> | |||
Add this in Model: | |||
<pre> | |||
class Restaurant < ActiveRecord::Base | |||
validates :name, presence: true, uniqueness: { case_sensitive: false } | |||
end | |||
</pre> | |||
then run Migration | |||
<pre> | |||
$ rake db:migrate | |||
</pre> | |||
and finally Seed the database: | |||
<pre> | |||
# db/seeds.rb | |||
Restaurant.create([ | |||
{ name: "The French Laundry" }, | |||
{ name: "Chez Panisse" }, | |||
{ name: "Bouchon" }, | |||
{ name: "Noma" }, | |||
{ name: "Taco Bell" }, | |||
]) | |||
</pre> | |||
===Step 3: Introduce AngularJS === | |||
Now we need to make use of the downloaded AngularJS, and move the files into app/assets/javascripts. | |||
Add it to Asset Pipeline, to tell our application we need AngularJs and to make sure it gets loaded before other dependent files. Add the following lines: | |||
<pre> | |||
//= require angular | |||
//= require main | |||
</pre> | |||
Now, AngularJS being an MVC framework requires its own '''Controller'''. Now we will create controller for it in directory /assets/javascripts/angular/controllers. Let's call the controller '''HomeCtrl''' | |||
<pre> | |||
# app/assets/javascripts/angular/controllers/HomeCtrl.js.coffee | |||
@restauranteur.controller 'HomeCtrl', ['$scope', ($scope) -> | |||
# Notice how this controller body is empty | |||
] | |||
</pre> | |||
===Step 4: Create Templates and Modify Controller === | |||
Make changes in the layout: | |||
<pre> | |||
<!-- app/views/layouts/application.html.erb --> | |||
<html> ==> <html ng-app="restauranteur"> | |||
<%= yield %> ==> <div ng-view> <%= yield %> </div> | |||
</pre> | |||
Make template for restaurants (this can be created anywhere), we are making it under public directory and create and index page: | |||
<pre> | |||
<!-- public/templates/restaurants/index.html --> | |||
<a href="/#">index</a> | |||
<ul ng-repeat="restaurant in restaurants"> | |||
<li><a ng-click="viewRestaurant(restaurant.id)">{{ restaurant.name }}</a></li> | |||
</ul> | |||
</pre> | |||
create Restaurant Index Controller | |||
<pre> | |||
# app/assets/javascripts/angular/controllers/RestaurantIndexCtrl.js.coffee | |||
@restauranteur.controller 'RestaurantIndexCtrl', ['$scope', '$location', '$http', ($scope, $location, $http) -> | |||
$scope.restaurants = [] | |||
$http.get('./restaurants.json').success((data) -> | |||
$scope.restaurants = data | |||
) | |||
] | |||
</pre> | |||
===Step 5: Adjust Routing in main.js=== | |||
Now modify routing so that new Restaurant Index page is called. | |||
<pre> | |||
# app/assets/javascripts/main.js.coffee | |||
@restauranteur = angular.module('restauranteur', []) | |||
@restauranteur.config(['$routeProvider', ($routeProvider) -> | |||
$routeProvider. | |||
when('/restaurants', { | |||
templateUrl: '../templates/restaurants/index.html', | |||
controller: 'RestaurantIndexCtrl' | |||
}) | |||
]) | |||
</pre> | |||
It's done! Now go to URI /#/restaurants to see list of Restaurants we added. | |||
==Testing== | |||
For testing we are going to use Rspec, Karma, and Jasmine framework. We start by first installing Karma server: | |||
====For Ubuntu==== | |||
<pre> | |||
sudo npm install -g karma | |||
sudo npm install -g karma-ng-scenario | |||
</pre> | |||
====For Windows==== | |||
Install via Git bash | |||
<pre> | |||
npm install -g karma | |||
</pre> | |||
===Step by step Testing Procedure=== | |||
Create Test Folder | |||
<pre> | |||
mkdir spec/javascripts | |||
</pre> | |||
Write Tests | |||
<pre> | |||
# spec/javascripts/controllers_spec.js.coffee | |||
describe "Restauranteur controllers", -> | |||
beforeEach module("restauranteur") | |||
describe "RestaurantIndexCtrl", -> | |||
it "should set restaurants to an empty array", inject(($controller) -> | |||
scope = {} | |||
ctrl = $controller("RestaurantIndexCtrl", | |||
$scope: scope | |||
) | |||
expect(scope.restaurants.length).toBe 0 | |||
) | |||
</pre> | |||
Add Configurations | |||
<pre> | |||
// spec/javascripts/restauranteur.conf.js | |||
module.exports = function(config) { | |||
config.set({ | |||
basePath: '../..', | |||
frameworks: ['jasmine'], | |||
autoWatch: true, | |||
preprocessors: { | |||
'**/*.coffee': 'coffee' | |||
}, | |||
files: [ | |||
'app/assets/javascripts/angular.js', | |||
'app/assets/javascripts/angular-mocks.js', | |||
'app/assets/javascripts/angular/controllers/RestaurantIndexCtrl.js.coffee', | |||
'app/assets/javascripts/angular/*', | |||
'spec/javascripts/*_spec.js.coffee' | |||
] | |||
}); | |||
}; | |||
</pre> | |||
Start Karma Server | |||
<pre> | |||
karma start spec/javascripts/restauranteur.conf.js | |||
</pre> | |||
Go to go to http://localhost:9876/ and test will run and be successful. | |||
==Comparison with other front-end frameworks== | ==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>: | The most popular front end frameworks these days include AngularJS, [http://backbonejs.org/ BackboneJS] and [http://emberjs.com/ EmberJS]. These can be compared on the basis of the following factors<ref>http://www.airpair.com/js/javascript-framework-comparison</ref>: | ||
===Community=== | ===Community=== | ||
Line 47: | Line 292: | ||
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. | 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 | So, if compared on the basis of community, AngularJS is the clear winner as there are more [http://stackoverflow.com/questions/tagged/angularjs StackOverflow questions] , [https://www.youtube.com/user/angularjs YouTube tutorials], [http://github.com/ 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=== | ===Framework Size=== | ||
Line 57: | Line 302: | ||
===Templating=== | ===Templating=== | ||
Front end frameworks generally use different kind of templates to style their code in a specific way. There are many kinds of [http://www.creativebloq.com/web-design/templating-engines-9134396 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 : | 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==== | =====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. | Angular's templating engine simply extends HTML syntax. It uses [https://docs.angularjs.org/guide/expression binding expressions] to bind variables between controllers and models. These binding expressions are enclosed by doubly curly braces. For example: | ||
<pre> | <pre> | ||
Line 72: | Line 318: | ||
</pre> | </pre> | ||
====EmberJS==== | =====EmberJS===== | ||
EmberJS uses Handlebars template engine. Handlebars is an extension of Mustache templating engine. It does not understand the | EmberJS uses [http://handlebarsjs.com/ Handlebars] template engine. Handlebars is an extension of [http://mustache.github.io/ Mustache] templating engine. It does not understand the [http://en.wikipedia.org/wiki/Document_Object_Model 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 [http://talks.erikbryn.com/htmlbars HTMLBars] which works on these flaws of the Handlebars. Since, HTMLBars is not yet ready therefore Handlebars is currently being used. For example: | ||
<pre> | <pre> | ||
Line 83: | Line 329: | ||
</li> | </li> | ||
{{/each}} | {{/each}} | ||
</ul> | |||
</pre> | |||
=====BackboneJS===== | |||
BackboneJS doesn't have any templating engine of its own but it can be integrated with many third party templating engines. Since, [http://underscorejs.org/ 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: | |||
<pre> | |||
<ul> | |||
<% _.each(recipes, function(recipe) { %> | |||
<li title="<%- recipe.name %>"> | |||
<%- recipe.desc %> | |||
</li> | |||
<% }); %> | |||
</ul> | </ul> | ||
</pre> | </pre> | ||
==Reference Material== | ==Reference Material== | ||
Following are the references to learn Ruby on which Rails is based : | |||
#[http://rubymonk.com/ Ruby Monk] | |||
#[http://rubylearning.com/ Ruby Learning] | |||
Following are the references to learn Rails : | |||
#[http://railsforzombies.org/ Rails for Zombies] | |||
#[http://www.railstutorial.org/book Ruby on Rails Tutorial] | |||
#[http://guides.rubyonrails.org/index.html Rails Guides] | |||
Following are the references to learn AngularJS : | |||
#[http://angularjs.org/ AngularJS] | |||
#[http://www.w3schools.com/angular/ AngularJS Tutorials] | |||
#[http://egghead.io/lessons AngularJS Training Videos] | |||
==References== | ==References== | ||
<references/> | <references/> |
Latest revision as of 04:37, 15 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.<ref>http://code.tutsplus.com/tutorials/5-awesome-angularjs-features--net-25651</ref>
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.<ref>https://www.codeschool.com/courses/shaping-up-with-angular-js</ref>
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.<ref>http://joelhooks.com/blog/2013/09/15/why-i-built-an-angularjs-training-site-on-rails</ref>
Getting Started<ref>http://sebastien.saunier.me/blog/2014/02/04/angular--rails-with-no-fuss.html</ref>
Prerequisites
AngularJS
Following are the pre-requisites for learning AngularJS:<ref>http://angular-rails.com/</ref>
- 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.
Download AngularJS
Download AngularJS from AngularJS website. We need to download file: angular-mocks.js
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 <ref>http://www.honeybadger.io/blog/2013/12/11/beginners-guide-to-angular-js-rails</ref>
Initial Setup
To create project:
$ rails new restauranteur --database=postgresql --skip-test-unit
To create PostgresSQL user:
$ createuser -P -s -e restauranteur
Add Rspec to your Gemfile:
gem "rspec-rails", "~> 2.14.0"
Install Rspec
$ bundle install $ rails g rspec:install
Create the database:
$ rake db:create
Step 2: Creating the Restaurant model
Our next step is to create a model class in Rails
$ rails generate scaffold restaurant name:string
Make sure that Restaurant Names are unique:
# db/migrate/[timestamp]_create_restaurants.rb class CreateRestaurants < ActiveRecord::Migration def change create_table :restaurants do |t| t.string :name t.timestamps end # Add the following line add_index :restaurants, :name, unique: true end end
Add this in Model:
class Restaurant < ActiveRecord::Base validates :name, presence: true, uniqueness: { case_sensitive: false } end
then run Migration
$ rake db:migrate
and finally Seed the database:
# db/seeds.rb Restaurant.create([ { name: "The French Laundry" }, { name: "Chez Panisse" }, { name: "Bouchon" }, { name: "Noma" }, { name: "Taco Bell" }, ])
Step 3: Introduce AngularJS
Now we need to make use of the downloaded AngularJS, and move the files into app/assets/javascripts.
Add it to Asset Pipeline, to tell our application we need AngularJs and to make sure it gets loaded before other dependent files. Add the following lines:
//= require angular //= require main
Now, AngularJS being an MVC framework requires its own Controller. Now we will create controller for it in directory /assets/javascripts/angular/controllers. Let's call the controller HomeCtrl
# app/assets/javascripts/angular/controllers/HomeCtrl.js.coffee @restauranteur.controller 'HomeCtrl', ['$scope', ($scope) -> # Notice how this controller body is empty ]
Step 4: Create Templates and Modify Controller
Make changes in the layout:
<!-- app/views/layouts/application.html.erb --> <html> ==> <html ng-app="restauranteur"> <%= yield %> ==> <div ng-view> <%= yield %> </div>
Make template for restaurants (this can be created anywhere), we are making it under public directory and create and index page:
<!-- public/templates/restaurants/index.html --> <a href="/#">index</a> <ul ng-repeat="restaurant in restaurants"> <li><a ng-click="viewRestaurant(restaurant.id)">{{ restaurant.name }}</a></li> </ul>
create Restaurant Index Controller
# app/assets/javascripts/angular/controllers/RestaurantIndexCtrl.js.coffee @restauranteur.controller 'RestaurantIndexCtrl', ['$scope', '$location', '$http', ($scope, $location, $http) -> $scope.restaurants = [] $http.get('./restaurants.json').success((data) -> $scope.restaurants = data ) ]
Step 5: Adjust Routing in main.js
Now modify routing so that new Restaurant Index page is called.
# app/assets/javascripts/main.js.coffee @restauranteur = angular.module('restauranteur', []) @restauranteur.config(['$routeProvider', ($routeProvider) -> $routeProvider. when('/restaurants', { templateUrl: '../templates/restaurants/index.html', controller: 'RestaurantIndexCtrl' }) ])
It's done! Now go to URI /#/restaurants to see list of Restaurants we added.
Testing
For testing we are going to use Rspec, Karma, and Jasmine framework. We start by first installing Karma server:
For Ubuntu
sudo npm install -g karma sudo npm install -g karma-ng-scenario
For Windows
Install via Git bash
npm install -g karma
Step by step Testing Procedure
Create Test Folder
mkdir spec/javascripts
Write Tests
# spec/javascripts/controllers_spec.js.coffee describe "Restauranteur controllers", -> beforeEach module("restauranteur") describe "RestaurantIndexCtrl", -> it "should set restaurants to an empty array", inject(($controller) -> scope = {} ctrl = $controller("RestaurantIndexCtrl", $scope: scope ) expect(scope.restaurants.length).toBe 0 )
Add Configurations
// spec/javascripts/restauranteur.conf.js module.exports = function(config) { config.set({ basePath: '../..', frameworks: ['jasmine'], autoWatch: true, preprocessors: { '**/*.coffee': 'coffee' }, files: [ 'app/assets/javascripts/angular.js', 'app/assets/javascripts/angular-mocks.js', 'app/assets/javascripts/angular/controllers/RestaurantIndexCtrl.js.coffee', 'app/assets/javascripts/angular/*', 'spec/javascripts/*_spec.js.coffee' ] }); };
Start Karma Server
karma start spec/javascripts/restauranteur.conf.js
Go to go to http://localhost:9876/ and test will run and be successful.
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 |