HTML

Magunkról

The blog of the Budapest based Digital Natives covers the topics such as technological challenges we meet in our work, also our solutions and developments related mostly to Ruby on Rails and e. g. JavaScript. You can read about project management methodologies, which drive our workflow, such as agile or scrum. We don’t forget to report about our work and free-time related events and activities.

Facebook

Címkék

2011 (1) 2012 (4) 2013 (5) 2014 (1) agency (1) agile (1) agilis (13) android (1) angel (2) anita (2) API (1) árazás (2) artisjus (1) balaton (1) bécs (1) becs (1) becslés (1) befekteto (1) befektető (7) bemutatkozás (1) berlin (1) beszédfelismerés (2) beszédtechnológia (1) bitbucket (1) borkóstoló (1) budapest.rb (1) célok (1) client (4) cloud (1) code hulk (1) coding (1) coin (1) concept (2) conference (1) continuous integration (1) cross browser (1) cross platform (2) csapat (4) csapatépítés (1) csocsó (1) David (1) ddb (1) deployment (3) design (2) dev (4) dev meeting (2) digital (1) diktálás (1) dojo (1) ebook (1) education (1) elemzés (3) elmélet (1) English (1) english (8) értékelés (1) értékesítés (3) extreme programming (1) fejlesztő (3) feliratozás (1) Friday (1) frontend (2) game (3) game of thrones (1) gerzson (2) hackfwd (2) heroku (1) hirdetés (1) hosting (1) icatapult (2) idcee (4) idea (1) implementation (2) inkubáció (9) ios (1) javascript (1) jenkins (1) jogdíj (1) kaizen (1) kalandpark (1) kanban (3) képzés (2) kijev (1) kipuedu (1) kirándulás (1) kocákzati tőkealap (1) kommunikáció (1) lean (2) LinkedIn (1) Logidok (1) mahasz (1) meetup (9) mindroom (2) Mitnick (1) mixgar (14) mobil (4) MVP (1) mvp (2) nabaztag (1) natives (1) olasz (1) open source (1) people search (1) piknik (1) planning (1) playertise (3) prága (1) presentation (1) product owner (1) product roadmap (1) project (1) prototípus (1) prototype (1) rabbit (1) rails (15) ruby (13) rupy (1) scrum (9) search API (1) series (2) sharewood (1) siker (2) sorozat (1) spaceship (1) speedinvest (1) startup (6) startup week (1) String (1) szerződés (1) szolgáltatás (2) taxi (3) taxitrust (3) taxtrust (1) techshow (2) testing (2) teszt (1) titanium (1) toborzás (2) tőke (2) toptal (1) trónok harca (1) ügyfél (1) UI (1) UML (1) UX (2) üzletiangyal (1) vagrant (1) varga anita (1) verseny (2) videó (1) videóarchívum (1) vienna (1) világhírnév (8) virtualbox (1) vm (1) vodka (1) web (8) wired (2) workflow (2) XP (1) xp (3) Címkefelhő

This post is created for those front-end developers who knows ruby and might want to try using it for front-end development.

JavaScript is a great language, I love it, I always have been, but it has it's problems:

  1. Not well designed - As many of you probably know the first version designed within a day that might be the main reason why is it not so understandable.
  2. No module system - This is the main problem with almost every major Library / Framework currently out there. As it seems this problem will persist in the future as well, as long as the new generation IE X will appears on the market - this would be the future version, which support JS modules.
  3. Gothas - I think this is pretty much covers all other aspects. These are the parts of the language what surely cause you headaches for a while without the proper understanding of how it works (scope, binding, prototype, etc..)

What I want

I'm going to describe some features what I would like to use in front-end world:

  1. The most important thing I want is to create Custom Elements just with JavaScript by extending native classes. So what this means is that you could extend the select element and create a new class that works and behaves like a select element but with a different tagname:
    class MySelect extends HTMLSelectElement {
      constructor(){
        super
        this.addEventListener('change',this.onChange.bind(this))
      }
    
      onChange(){
        alert(this.value)
      }
    }
    
    mySelectInstance = new MySelect
    document.body.appendChild(mySelectInstance)
    // <my-select></my-select>
    
    This would be awesome, but using in real life seems impossible. Implementation would take ages even if there be a draft created by the W3C to speed up the process, the older versions still need to be supported. So in one expression: total nightmare.
  2. Manage dependencies the way it makes sense. Sprockets / Mincer provides this in the form of require keywords at the top of the file. Basically it will produce one file with dependencies loaded in order, also could minify code.
  3. Getters and Setters as default. Sure you can do this with JavaScript but there are no conventions that specify it's use, also you can't declare getters and setters for classes.

As I'm constantly thinking of these features I'm also looking for a solution for a long time. Finally, I found something, so keep reading if you still interested.

Opal

About a year ago I found Opal. This is a Ruby to JavaScript compiler, and because I learned Ruby and I love the language I felt like "Yeeey! This is Awesome!" . Then I tried it and realised it wasn't working properly. There were problems with scoping and language features, there was no require and stuff, it wasn't ready at all. When I checked the site again a few months later, I found the product in more matured shape with the functions what make it more useable and effective:

  • Ruby in the Browser
  • It has Sprockets support (Yey!), and it uses require statements (2. featured solved).
  • It has RSpec support, which makes unit testing a breeze.
  • You can load any third party gems easily.
  • Have most of Corelib and some of Stdlib.
  • Fits in with Ruby development on the back end via Rack.

How it is done?

Easy DOM

Opal has no DOM bindings, because it's just a compiler, but it is easy to wrap any Native JavaScript object to a variable and thus a class instance with the backticks evaulation:

class Node
  attr_reader :node

  def initialize(node)
    @node = node
  end

  def appendChild(otherNode)
    `@node.appendChild(#{otherNode.node})`
  end
end

body = Node.new `document.body`

This snippet defines a Node class which sets the instance variable node to the argument what is passed on initialise. The last line will set the body variable an instance of the Node class with the `document.body` as the node. We would need to do this as a library, and then it we should have easy DOM bindings.

Ruby features

So what we did is that we wrapped a Native Node into a Ruby Class, cool right? Yes but we can take this further: we can use Ruby to define the `<<` operator that will append the given node the the current node.

def appendChild(otherNode)
  ...
end
alias :<<, :appendChild

 Well that was easy :). At the end what I love about Ruby, I can now use at the front end, some examples:

  • Can use question marked methods. `body.empty?`
  • attr_accessor, alias and such
  • DSLs
  • Module and Class inheritance
  • Getters and Setters
  • much more

Custom Components

Remember what the first feature was that I wanted? Custom Components.

Now with this wrapping system it's realtivly easy:

# We extend the Node class we created before
class Component < Node
  def initialize
    # we are not calling super but are getting the class name
    tagName = self.class.name.split("::").pop
    # then we create the Native Node
    @node = `document.createElement(#{tagName})`
  end
end

# now we can extend that class to create a custom element
class MyElement < Component
  # and define custom logic
  def select
    `@node.addClass('selected')`
  end
end

# and when you create a MyElement
myEl = MyElement.new
body << myEl
# <body><myelement></myelement></body>
myEl.select

If we work on this some more we can add a DSL to this and create a Library / Framework / System that in the end create components with inherintence, thus solving my first feature.

Does it worth it?

The main question is that is this worth it in the end? Well here are some pros and cons.

Pros:

  • You can use Ruby in the browser instead of JavaScript
  • You can manage your dependencies
  • You can use ruby only gems
  • There is a standard for building your code (Sprockets)
  • There is a standard for testing your code (RSpec, Cucumber)

Cons:

  • Bigger File size because of the Runtime dependency (44kb gzipped)
  • Have to wrap external Libraries
  • Harder to find Ruby developers than JavaScript developers

I think it is worth it if you know Ruby, and it's absolutely worth it if you are a Rails developer, but it's up to you in the end. I'll definatly try to use this whenever I can.

What's next?

At the beginning of this article I have three concerns mentioned related to front-end development: Custom Components, Module System, Getters and Setters. All of these can be solved based on what I wrote.

The next step for me is to write a Gem that contains wrapper for the most commonly used front end classes: DOM, LocalStorage, Requests and Websockets, custom components and a basic MVC architecture.

komment

A bejegyzés trackback címe:

https://digitalnatives.blog.hu/api/trackback/id/tr1006184139
Nincsenek hozzászólások.
süti beállítások módosítása