3

尝试使用 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。我在我的网络浏览器中使用 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);

有任何想法吗?

4

3 回答 3

2

最初,我认为这可能是因为您不需要opal-jquerygem(我假设您已经安装了它)。另一个猜测:也许您<script src="opal-jquery.js"></script>的 HTML 文件中需要 a ?

于 2013-05-19T02:36:21.573 回答
2

尝试将opalandopal-jquery直接放入您的 html 中,并将需求放在 之外app.rb,您可以从http://cdnjs.com获取它们。

否则我想看看编译好的app.js(你可以把它放在一个gist中)。

于 2013-05-23T16:44:04.733 回答
0

看看这里:https ://github.com/opal/opal-jquery/issues/26#issuecomment-25285375

由于蛋白石作者/管理员,我遇到了完全相同的问题并得到了分类。

于 2013-09-28T12:44:23.667 回答