JavaScript-Opal RB中未初始化的常量Object :: Element

尝试Opal / JQuery.我的app.rb文件如下所示:

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

我使用从站点到app.js的示例将其编译.我在Web浏览器中使用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>

当我打开页面时,我什么都看不到.控制台显示错误跟踪:

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)

JS输出文件如下所示:

 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);

有任何想法吗?

解决方法:

尝试将opal和opal-jquery直接放在html内,并将require排除在app.rb之外,您可以从http://cdnjs.com获取它们.

否则,我想查看已编译的app.js(可以将其放入gist).

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...