inesita-rb / inesita

Frontend web application framework in Ruby using Opal.
https://inesita.fazibear.me/
MIT License
259 stars 15 forks source link

Jquery and inesita #40

Closed jeezs closed 6 years ago

jeezs commented 6 years ago

Hello, Just add opal-jQuery to inesita and had a few issue, the most annoying issue is when I reload the page, any event add with query are ignore until I quit and re launch the browser. Here is an example code that stop working after a page reload. div class: "item" do "kool!!" end Element.find('.item').on :click do alert('good!') end Do you have any clue? Thanks

fazibear commented 6 years ago

If you really want to use jquery with virtual-dom based framework like Inesita, you need to understand one important thing. You have to attach each event every time virtual-dom renders. There are post render hooks that can be used.

Anyway using jquery with virtual-dom is a bad idea in my opinion.

jeezs commented 6 years ago

Thanks for your answer. I understand jquery is not a good choice with virtual dom. Then how can I proceed to obtain a simple solution for get a div and assign a class on a click using virtual dom. I’ve looked in inesita example and doc didn’t find any example code that can modify a dom object based on it’s ID. Can you give me just a small example, I am a early begginers with these technologies. Thanks anyway for the time spent answering my questions.

fazibear commented 6 years ago

It's very easy you can render it dynamically and fetch it from store for example:

class Store
  include Inesita::Injection

  attr_accessor :class_name

  def init
    @classname = "something"
  end

  def set_class_name(sth)
    @classname = sth
  end
end

class Something
  include Inesita::Component

  def dynamic_classname 
    store.classname
  end

  def on_click
    store.set_classname("other name")
  end

  def render
    div class: dynamic_classname, onclick: method(:on_click) do
      text "Hello I'm Inesita"
    end
  end
end
jeezs commented 6 years ago

Perfect. Big thanks for your help