Blog

  • Stephen Huang

    How to structure JavaScript code when using AJAX in Rails

    By Stephen Huang,

    AJAX is a great solution that allows you to send and retrieve data from the server and update the page without having to reload the entire page.

    In this article I will show you two strategies in Rails to use the data from server by executing JavaScript code: AJAX Callback Function and Server-generated JavaScript Responses. Then I'll explain how they work, and describe their benefits.

  • Stephen Huang

    ActionCable for Rails and Angular JS 1.x

    By Stephen Huang,

    Action Cable is an awesome feature that uses Web Sockets to realise a real time application in Rails, and includes both the back-end and the front-end. In this article, we will use only the server side of Action Cable in Rails and client-side in AngularJS 1.x. This is not a step-by-step tutorial, but it is intended to help you to understand the purpose of each step.

    The first thing we need to do is enable Action Cable in our back-end app. The simplest way is to mount action cable in the route:

  • Adam Davies

    The Quirky Array Constructor and a Use for Holey Arrays in ES6

    By Adam Davies,

    In JavaScript, an Array isn't like your typical array: it's an object with keys that are indexes. What this means is that you need to be aware that the length can be more than the number of elements and that there can be "holes".

    In day-to-day development you'll probably just build up arrays using the literal syntax, like so:

  • Sebastian Porto

    ES6 classes and JavaScript prototypes

    By Sebastian Porto,

    ES6 introduces the class keyword. At first glance this looks like a completely new object model that follows classical languages like Java or Ruby, but behind the new syntax there is nothing new. The class syntax just provides an alternative way to create plain old JavaScript objects.

    In this post I want to explain ES6 classes in the context of the JavaScript objects they produce.

  • Sebastian Porto

    Rails with Webpack - Why and How

    By Sebastian Porto,

    The Asset Pipeline (Sprockets) is the canonical way of packaging assets in Rails, it makes things very easy for us as it provides a very clear way on how to load our assets and how to bundle them for production.

    I believe the Asset Pipeline is a good approach for most Rails applications, but there are times when it makes things harder and shows limitations.

  • Sebastian Porto

    Rails server sent JavaScript considered harmful

    By Sebastian Porto,

    Rails provides a very convenient way of sending forms via AJAX using the Rails UJS helpers e.g.

    Then in your controller you can process that form and respond with javascript:

  • Sebastian Porto

    Lessons learnt by building Single Page Applications

    By Sebastian Porto,

    The last 5 years has seen an explosion of rich front-end applications (also referred as SPA - Single Page Application) and during this time I have had the luck of working on many projects like this.

    I have worked on small single page apps and a few big ones too. Built with many different things like Adobe Flex, jQuery, Backbone, Angular, CanJS. Thus I believe I have a bit of experience to share.

  • Sebastian Porto

    A better way to modularise your JS in Rails

    By Sebastian Porto,

    When building a Rails application with considerable JavaScript you might get to a point where the loading order of your JavaScript files matters. Take for example the following code:

    In this code we expect to find an object in the App.Vehicle namespace, so obviously the code that defines App.Vehicle has to be loaded beforehand. So we will need to be careful with the loading order of our JS files. Because of this you could end up with a JavaScript manifest file that has dozens of entries e.g.

  • Sebastian Porto

    Dealing with timezones effectively in Rails

    By Sebastian Porto,

    We all have to deal with timezones in our Rails app sooner or later (probably sooner). In this blog post I want to share some of the tricks I have learned to deal with timezones effectively in Rails.

    Unless you are confident that you will never need to deal with timezones, you should think about them sooner rather than later, building your application in a way that accommodates for them from the beginning. This is one of those things that can be very difficult to include later, so I don't think it is a premature optimisation.

  • Sebastian Porto

    Best practice on naming JavaScript hooks in your views

    By Sebastian Porto,

    If you are building an application with JavaScript on the front end you will probably need to hook your JS code to html elements in order to respond to events. This is very common when building apps with jQuery, Backbone, CanJS and the like. E.g.

    In this case the class btn_view is the hook that JS uses for listening to or manipulating the DOM. The choice of what to use for these hooks may seem inconsequential at first but it is an important decisions that can aid a lot with the maitainability of your application.