TitaniumでjQueryを使う

windowとかdocumentオブジェクトがないせいで動かないので、空のオブジェクトを定義してやるという方法。
そんなやり方でいいのか。

jQuery 1.5だと上のやり方のままでは動かなかったのでちょっと修正して動いたのが以下のコード。

var window = {};
var document = { 
    getElementById: function(){}, 
    createComment: function(){}, 
    documentElement: { 
        insertBefore: function(){}, 
        removeChild: function(){} 
    }, 
    createElement: function(e){ 
        return obj = { 
            innerHTML: "", 
            appendChild: function(){}, 
            getElementsByTagName: function(){ return {}; }, 
            style: {} 
        },
    }
}; 
var navigator = { userAgent: "" }; 
var location = { href: "" };
window.document = document; // ここ追加

Ti.include("jquery-1.5.js");

// 動作確認
o1 = {width: 50};
o2 = {height: 100};
o3 = window.jQuery.extend({}, o1, o2);

alert("width:" + o3.width + ", height:" + o3.height);

動いた。

しかしDOMがない環境ではjQueryを使う価値はそんなに高くないかもしれない。

2011/03/01追記

Androidで動かすとぬるぽで落ちちゃうらしい。
via Twitter / 濱田 孝治: これandroidだと、実行時にjavaがぬるぽでこ …

TitaniumでjQueryを使う” に対して1件のコメントがあります。

コメントは受け付けていません。