1

ちょっと今私はjQueryを使用していて、プリロードされたajaxのものを少し保持するためのいくつかのグローバル変数があります(ページを素晴らしく速く表示するためにプリロードされています):


$.get("content.py?pageName=viewer", function(data)
    {viewer = data;});
$.get("content.py?pageName=artists", function(data)
    {artists = data;});
$.get("content.py?pageName=instores", function(data)
    {instores = data;});
$.get("content.py?pageName=specs", function(data)
    {specs = data;});
$.get("content.py?pageName=about", function(data)
    {about = data;});

ご覧のとおり、DRYの原則に大きく違反していますが、それを修正する方法がわかりません...何かアイデアはありますか?

多分配列?

4

6 に答える 6

6

jQuery each メソッドを使用してページ名の配列を反復処理し、グローバル (ウィンドウ スコープ内) 変数を設定します。

jQuery.each(
    ["viewer", "artists", "instores", "specs", "about"],
    function (page) {
        $.get("content.py?pageName=" + page,
            new Function("window[" + page + "] = arguments[0]"));
    }
);

更新:実際には、「新しい関数」も必要ありません:

jQuery.each(
    ["viewer", "artists", "instores", "specs", "about"],
    function (page) {
        $.get("content.py?pageName=" + page, function () { window[page] = arguments[0]; });
    }
);
于 2008-10-14T16:46:11.010 に答える
5

eval()あなたはこれのためにまたはを必要としませんFunction()。ご想像のとおり、配列はうまく機能します。

(function() // keep outer scope clean
{
   // pages to load. Each name is used both for the request and the name
   // of the property to store the result in (so keep them valid identifiers
   // unless you want to use window['my funky page'] to retrieve them)
   var pages = ['viewer', 'artists', 'instores', 'specs', 'about'];

   for (var i=0; i<pages.length; ++i)
   {
      // "this" refers to the outer scope; likely the window object. 
      // And will result in page contents being stored in global variables 
      // with the same names as the pages being loaded. We use the with({})
      // construct to create a local scope for each callback with the
      // appropriate context and page name.
      with ({context: this, pageName: pages[i]})
         $.get("content.py?pageName=" + pageName, function(data)
            {context[pageName] = data;});
   }

})(); // close scope, execute anonymous function

// at this point, viewer, artists, etc. are populated with page contents 
// (assuming all requests completed successfully)
于 2008-10-14T17:06:02.077 に答える
2

新しい関数を使用して評価を回避できます。

var names = ['viewer', 'artists', 'instores', 'specs', 'about'];
for (var i = 0; i < names.length; i++)
   $.get("content.py?pageName=" + names[i], new Function('data', names[i] + ' = data;'));

tbhですがそれほど良くはありません

于 2008-10-14T16:19:26.943 に答える
0

そのページを 1 回だけ呼び出して、テキストの代わりに json オブジェクトを返すことができます

{
viewer:'me',
artists:'you',
instores:'instores',
specs:'specs',
about:'about'
}

そして、それを評価する 今、あなたはあなたのサーバーをN回呼び出しているので、これはすべてを遅くします。あなたのロジックを再考する必要があります!

PS。私が書いているように、私はRoBorgの答えを見ました。新しい関数を使用するときは、内部でevalを使用しているため、使用したい場合はそれを使用してください(ブラウザによっては高速です)

于 2008-10-14T16:35:22.200 に答える
0

これは eval を使用していませんが、少し冗長です。

function get_content(name){
   $.get("content.py?pageName=" + name, function(data){ window[name] = data;});
}

var names = ['viewer', 'artists', 'instores', 'specs', 'about'];
for (var i = 0; i < names.length; i++)
    get_content(names[i]);

しかし、回答者の1人が良い点を指摘しました。おそらく、これらすべてのリクエストを1つにまとめてみてください。そうしないと、ページの各リクエストで動的コンテンツに対してサーバーが6回ヒットします。

于 2008-10-14T17:17:35.737 に答える
0

これらの提案された解決策のほとんどは、evalの使用を避けています。その慣行は、Doduglas Crockford の「JavaScript プログラミング言語のコード規則」でさらに強化されています。

「eval は悪です

eval 関数は、JavaScript で最も誤用されている機能です。避けてください。

eval にはエイリアスがあります。Function コンストラクターを使用しないでください。」

于 2008-10-17T13:41:02.393 に答える