Search code examples
javascriptrubyopalrb

uninitialized constant Object::Element in Opal RB


Trying my hand at Opal/JQuery. My app.rb file looks like this:

require 'opal'
require 'opal-jquery'

class HTMLObject
  def initialize

  end

  def write_to_body

  end
end


class HTMLParagraph < HTMLObject
  attr_accessor :inner_html
  def initialize(text)
    @inner_html= text
  end

  def write_to_body

    @body = Element.find("#body")
    @body.append(Element("<p>#{@inner_html}"))
  end
end

p = HTMLParagraph.new("hello world")
p.write_to_body

I compile it using the example from the site to app.js. I run it in my web browser with index.html:

<!DOCTYPE html>
<html>
<head>
    <script src="jquery-1.10.1.min.js" type="text/javascript"></script>
    <script src="opal.js" type="text/javascript"></script>
    <script src="opal-jquery.min.js" type="text/javascript"></script>
    <script src="opal-parser.js" type="text/javascript"></script>
    <script src="app.js" type="text/javascript"></script>
    <title></title>
</head>
<body>

</body>
</html>

When I open the page I do not see anything. The console reveals this error trace:

Uncaught NameError: uninitialized constant Object::Element opal.js:1531
def.$backtrace.backtrace opal.js:1531
def.$raise opal.js:1279
def.$const_missing opal.js:575
Opal.cm opal.js:255
def.$write_to_body app.js:44
(anonymous function) app.js:51
(anonymous function)

The JS output file reads thus:

 function(__opal) {
  var p = nil, _a, _b, self = __opal.top, __scope = __opal, nil = __opal.nil, $mm = __opal.mm, __breaker = __opal.breaker, __slice = __opal.slice, __klass = __opal.klass;
  (function(__base, __super){
    function HTMLObject() {};
    HTMLObject = __klass(__base, __super, "HTMLObject", HTMLObject);

    var def = HTMLObject.prototype, __scope = HTMLObject._scope;

    def.$initialize = function() {

      return nil;
    };

    def.$write_to_body = function() {

      return nil;
    };

    return nil;
  })(self, null);
  (function(__base, __super){
    function HTMLParagraph() {};
    HTMLParagraph = __klass(__base, __super, "HTMLParagraph", HTMLParagraph);

    var def = HTMLParagraph.prototype, __scope = HTMLParagraph._scope;
    def.inner_html = def.body = nil;

    def.$inner_html = function() {

      return this.inner_html
    }, 
    def['$inner_html='] = function(val) {

      return this.inner_html = val
    }, nil;

    def.$initialize = function(text) {

      return this.inner_html = text;
    };

    def.$write_to_body = function() {
      var _a, _b, _c;
      this.body = ((_a = ((_b = __scope.Element) == null ? __opal.cm("Element") : _b)).$find || $mm('find')).call(_a, "#body");
      return ((_b = this.body).$append || $mm('append')).call(_b, ((_c = this).$Element || $mm('Element')).call(_c, "<p>" + (this.inner_html)));
    };

    return nil;
  })(self, ((_a = __scope.HTMLObject) == null ? __opal.cm("HTMLObject") : _a));
  p = ((_a = ((_b = __scope.HTMLParagraph) == null ? __opal.cm("HTMLParagraph") : _b)).$new || $mm('new')).call(_a, "hello world");
  return ((_b = p).$write_to_body || $mm('write_to_body')).call(_b);
})(Opal);

Any ideas?


Solution

  • Try putting opal and opal-jquery directly inside your html, and leaving the requires out of app.rb, you can grab them from http://cdnjs.com.

    Otherwise I'd like to see the compiled app.js (you can put it in a gist).