2012-02-23 11 views
4

jqueryをjqueryで使用していますが、うまくいきます。しかし、私は自分のコードを少しずつモジュール化しようとしていますので、私は自分自身を繰り返さないので、html(DOM)をいくつか取り、jqueryで微調整して元に戻します。しかし、私は私の結果を返すことができないので、それを呼び出すvarに割り当てます。私はおそらく正しい場所に戻っていないだろうが、私はちょうどもしそうなら明らかではない。少しの助けを使うことができました。単純なjsdom関数から値を返すにはどうすればよいですか?

ここでは、コードです:

function tweakIt(html_in, callback){ 
    var jsdom = require('jsdom'); 
    jsdom.env({ 
    html: html_in, 
    scripts: [ 
     '../public/javascripts/jquery-1.7.1.min.js', 
    ], 
    done: function(errors, window) { 
     var $ = window.$; 
     // do some jquery magic and manipulate the dom 
     $('body').append('<div>foo</div>'); 

     console.log('Freshly Manipulated HTML: '+ $('body').html()); // it logs perfectly 
     callback($('body').html()); // instead of a return, pass the results to the callback 
    } 
    }); 
} 

var oldhtml = '<html><body><div>some text</div></body></html>'; 
var newhtml = tweakIt(oldhtml, function(newstuff){ 
    console.log(newstuff); // woohoo! it works! 
}); 

答えて

5

:それはので、ここではなく、戻りのコールバックを使用して行われるべき方法です、実際に非同期問題でした

function tweakIt(html_in){ 
    var jsdom = require('jsdom'); 
    jsdom.env({ 
    html: html_in, 
    scripts: [ 
     '../public/javascripts/jquery-1.7.1.min.js', 
    ], 
    done: function(errors, window) { 
     var $ = window.$; 
     // do some jquery magic and manipulate the dom 
     $('body').append('<div>foo</div>'); 

     console.log('Freshly Manipulated HTML: '+ $('body').html()); // it logs perfectly 
     return $('body').html(); // this isn't returned to where I called tweakIt() from, why not? 
    } 
    }); 
} 

var oldhtml = '<html><body><div>some text</div></body></html>'; 
var newhtml = tweakIt(oldhtml); // never gets set because nothing gets returned, why? 

EDIT私は戻り値を使ってこれを行うことはできないと思います。done:は非同期関数です。 tweakItにコールバックを追加して、パラメータとして送信することで新しいhtmlを取得してください。

tweakIt(oldHtml, function(newHtml) {/*use the result here*/})

+0

OK、しかし、どのように私は '行わからコールバックにnewHtmlを送ってください:'? – k00k

+0

気にしない、私はそれを理解した。どのようにして行うべきかを示すためにOPを編集します。ありがとう! – k00k

+1

あなたがtweakIt関数 'callback'で2番目のパラメータを指定したと仮定した場合、done関数のreturn文の代わりに次のようなことをします:' callback($ body).html()) ' –

0

JSDOM APIの新バージョンは、もはや「完了」コールバックのオプションが含まれていません。

DOM変数にアクセスするための 'poor man's callback'が設定された後にのみ書きました。

function getSomeDOMVar(callback) { 
 

 
    const jsdom = require("jsdom"); 
 
    const { JSDOM } = jsdom; 
 

 
    const dom = new JSDOM(` 
 
    <!DOCTYPE html> 
 
    <html> 
 
     <body> 
 
      <script> 
 
       var result; // globally scoped, undefined var, accessible in the node scope as dom.window.result 
 

 
       function doSomething() { 
 
        // your code goes here 
 
       } 
 

 
       // then assign the data you want back to your the globally scoped var 
 
       result = doSomething(); 
 

 
      </script> 
 
     </body> 
 
    </html> 
 
    `, { 
 
     runScripts: "dangerously", 
 
     resources: "usable" 
 
    }); 
 

 
    // poor man's callback 
 
    function waitForVar() { 
 
     if (typeof dom.window.result !== 'undefined') { 
 
      cb(dom.window.result); 
 
     } 
 
    } 
 
    setTimeout(waitForVar, 1000); 
 
} 
 

 
getSomeDOMVar(function(result) { 
 
    console.log(result) 
 
});

関連する問題