2013-05-18 54 views
3

在Opal/JQuery中試用我的手。我的app.rb文件如下所示:未初始化的常量Object :: Opal中的元素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); 

任何想法?

回答

2

試着把opalopal-jquery直接放到你的html裏面,並且把需求從app.rb裏面拿出來,你可以從http://cdnjs.com上抓住它們。

否則我想看看編譯的app.js(你可以把它放在gist)。

+0

當我在cdnjs.com上搜索opal-jquery時,它不返回任何結果。當我搜索opal時,它返回opal.js和opal-parser.js – RedMage

+0

你會上傳opal-jquery.js到CDN嗎? – RedMage

+0

當然,我會做一個公關,注意合併需要一些時間,一旦準備就緒,我會在源頭添加一個鏈接 –

2

最初,我想這可能是因爲你不需要opal-jquery寶石(我假設你已經安裝了它)。另一個猜測:也許你需要在你的HTML文件中的<script src="opal-jquery.js"></script>

+0

我最初在我的編譯器文件中有它的要求。我將「require'opal-jquery'」行移至「require'opal'」之後的app.rb,並且在「重新編譯」並重新加載後仍然拋出相同的控制檯錯誤index.html – RedMage

+0

我確實安裝了opal-jquery gem 。 – RedMage

+0

已編輯,請再試一次... –