AngularJS vs Turbolinks
Turbolinks , as well as AnguluarJS, can be used to make a web application respond faster in the sense that in response to user interaction, something happens on the web page without reloading and reloading the entire page.
They differ in the following respects:
AngularJS helps you create a rich client application where you write a lot of JavaScript code that runs on the client machine. This code makes the site interactive for the user. It interacts with the server on the server side, that is, with the Rails application using the JSON API.
Turbolinks , on the other hand, helps make a site interactive without the need for JavaScript code. This allows you to adhere to server-side Ruby / Rails code and still “magically” use AJAX to replace and, therefore, redefine only those parts of the page that have changed.
Where Turbolinks is strong in that you can use this powerful AJAX engine without any manual actions and just Ruby / Rails code, a step may arise as your application grows where you would like to integrate a JavaScript framework such as AngularJS ,
Especially at this intermediate stage, when you would like to consistently integrate AngularJS into your application, one component at a time, it might make sense to run Angular JS and Turbolinks together.
How to use AngularJS and Turbolinks together
Use callback for Angular manual bootstrap
In Angular code, you have a line defining your application module, something like this:
This code runs when the page loads. But since Turbolinks simply replaces part of the page and prevents the entire page from loading, you need to make sure that the Angular application is properly initialized (“loaded”) even after such partial reboots made by Turbolinks. Thus, replace the above module declaration with the following code:
Do not download automatically
In tutorials, you often see how to automatically download an Angular application using the ng-app attribute in your HTML code.
<html ng-app="YourApplication"> ...
But using this mechanism together with the manual bootstrap shown above will cause the application to load twice and therefore block the application.
So just remove this ng-app attribute:
<html> ...
additional literature
fiedl Mar 18 '13 at 23:34 2013-03-18 23:34
source share