opal / opal-browser

Browser support for Opal.
MIT License
115 stars 36 forks source link

Instructions don't work #59

Closed stugol closed 4 years ago

stugol commented 8 years ago

I'm trying to run code similar to the example:

:opal
  $document.ready do
    alert "blah"
  end

I get undefined method 'ready' for nil.

Similarly:

:opal
  alert "blah"

I get undefined method 'alert' for main

meh commented 8 years ago

Did you require 'browser'?

stugol commented 8 years ago

Yup.

elia commented 8 years ago

@stugol is it in a haml filter?

stugol commented 8 years ago

Yup. I have the following filter code:

module Haml::Filters::Opal
  include Haml::Filters::Base

  def render_with_options ruby, options
    text = ::Opal.compile(ruby)
    type = (options[:format] == :html5) ? '' : " type=#{options[:attr_wrapper]}text/javascript#{options[:attr_wrapper]}"

    text.rstrip!
    text.gsub!("\n", "\n    ")

    <<HTML
<script#{type}>
  //<![CDATA[
    #{text}
  //]]>
</script>
HTML
  end
end
hmdne commented 4 years ago

This looks like there's no browser loaded on the browser side, only opal-browser server side. You would either need to add require "browser" in some HAML filter or add some <script src='opal-browser.js'></script> with a pre-compiled opal-browser (November 2019: I don't think there are any up-to-date pre-compiled distributions of opal-browser as of yet, we will probably do it with the next release)

I'm closing this issue in a process of tidying up issues of this project. Please feel free to reopen it if it is still relevant :)