0.02
No commit activity in last 3 years
No release in over 3 years
There's a lot of open issues
virtual-dom wrapper for opal
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

> 0

Runtime

> 0
 Project Readme

Virtual Dom support for Opal

Gem Version Code Climate

Description

Opal wrapper for virtual-dom javascript library. For more information see virtual-dom wiki page.

Usage

Server side (config.ru, Rakefile, Rails, Sinatra, etc.)

require 'opal-virtual-dom'

Browser side

require 'opal'
require 'browser'     # not required
require 'virtual_dom'

# if you're using browser and you want to have events and elements wrapped
require 'virtual_dom/support/browser'

class SampleList
  include VirtualDOM

  def initialize(elements = [])
    @elements = elements
  end

  def create_hook(node, name, previous)
    puts "I'm created: #{node}"
  end

  def render
    # You can use chained methods
    # to add class to elements
    # use bang method to define element id

    # <p id="example">
    p.example! do
      # <ul class="simple-list">
      ul.simple_list.list(hook: Hook.method(method(:create_hook))) do
        @elements.each do |string|
          li class: class_names({empty: string.empty?}) do
            text string
          end
        end
      end
    end
  end
end

$document.ready do
  list = SampleList.new(%w(one two three)).render.to_vnode
  back = SampleList.new(%w(three two one)).render.to_vnode

  root_node = VirtualDOM.create(list)
  $document.body.inner_dom = root_node

  diff = VirtualDOM.diff(list, back)
  VirtualDOM.patch(root_node, diff)
end

Thank you!

Become Patreon