Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use a js framework? [closed]

We have a ruby on rails website which uses a lot of rail's built in remote ajax with js templates. We have new requirements that are taking us in the direction of a single page application where entire blocks and columns of the page will be ajax based.

Should we continue to use rails ajax infrastructure or start incorporating a js framework like angularjs? In other words what are the principle evaluation criteria for determining the need for a js framework?

like image 643
phil Avatar asked Jul 15 '14 15:07

phil


People also ask

Should I use a JS framework?

If What You're Working on Is Simple Truth be told, JavaScript frameworks are best used on more complicated projects for applications at scale. If you're working on something simple and small, then a framework is going to be overkill.

When should I use the front-end framework?

Front-end frameworks are a powerful tool for developing complex user interfaces. They encourage you to build out a maintainable, modular, standalone architecture that makes it easy to build your application and collaborate with other developers.

Is a front-end framework necessary?

Absolutely. Not all web applications need front-end frameworks. You can easily build the front end for a simple web application using just HTML, CSS, and JavaScript. This front end tech stack for web development is sufficient for building a web app with basic dynamic controls that can respond to the user requests.

Why do we use JS frameworks?

JavaScript frameworks are an essential part of modern front-end web development, providing developers with tried and tested tools for building scalable, interactive web applications.


2 Answers

Personally, I think that once you have ajax responses that need to potentially update multiple non-adjacent areas of the page, it's definitely time to introduce a JS framework. Server generated javascript (like what the rails helpers give you) is convenient for very simple things, but once things get more complex than that, you'll be better off just returning JSON data from rails and having the front end figure out from that which parts of the page should get updated.

There's no reason you have to be all one or the other, either. You can continue using Rails' JS helpers for the simple things, but on pages where it makes sense, bring in something like KnockoutJS, Backbone or Angular. I introduced KnockoutJS this way to an existing app and it's worked out pretty well. It helped that it didn't depend on a specific version of jQuery and also supports older browsers.

One thing you'll need to decide is whether to add a JS framework via a rubygem, or just download it and add it to your asset pipeline. The first way might be simpler, but it will also tie you to the version included with the gem, which might leave you a couple versions behind at some point. If I were starting a new app, I'd probably not use any of rails' javascript helpers, and maybe not even the asset pipeline, and use other tools like grunt to do that instead. It's almost a 'separation of concerns' with more work up front, versus 'batteries included' tradeoff.

like image 60
sockmonk Avatar answered Sep 24 '22 00:09

sockmonk


A javascript framework like Angular is a good idea when you can use a more backend JSON API for your data source, keeping the rails code lighter. If you're having to do a lot of back and forth communication through AJAX, Angular or even Backbone can make this quite a bit cleaner. It really comes down to what kind of application you're building, how you think the front end should be structured, and if a framework can help with that.

Since you do already have your code in the remote true with js.erb going on, you can stick with that. I would only really switch at this point if it's becoming a huge pain. As changing frameworks/architecture in an existing app can be a fair bit of work. Especially if you're new to angular.

Recently I worked on a project at work that was going to be more client side heavy. I ended up using handlebars for some template rendering, and then just a few singleton objects. The reason for this though was because we aren't persisting very much data. Just a couple fields into a cookie which is done with an ajax request, and then a single JSON end point for the data the front end & those singletons utilize.

like image 25
agmcleod Avatar answered Sep 20 '22 00:09

agmcleod