1
var a = { property: ((type == 'files') ? 'id' : ((type == 'folders') ? 'name' : '')) };     // have to create a dummy object property...
return_json[type].push({ a['property']: Content[type][index][a['property']] });

クライアント側クエリの JSON を返そうとしていますが、返されたオブジェクトのキーが変数に基づいている必要があり、機能していません。ダミー オブジェクトを作成するとうまくいくという印象を受けましたが、うまくいきません。

Firefox は、2 行目の ['property'] のプロパティ ID の前に、エラー、missing : を表示します。

ありがとう!


必要な場合に備えて、すべてのコードを次に示します。はい、主にオブジェクト指向です。これらの 2 つの関数は、ユーザーが選択したファイルのリスト (これはファイル マネージャーです) を取得するために使用され、ファイルを削除するための ajax 要求またはユーザーが何をしようとしているのかを示します。しかし、オブジェクト全体ではなく、各ファイルの ID のみを返したい場合があります。ここで問題が発生します。ファイルの場合は「id」を返す必要がありますが、フォルダーの場合は「名前」を返す必要があります。

GetSelectedJSON: function(which = 0, onlyselected = true, justids = false) {            // add another param to only get IDs!

    var return_json = { files: [], folders: [] }

    this.ForEachItem(which, onlyselected, function(index,type) {

        var type = ((type == 1) ? 'files' : ((type == 2) ? 'folders' : function(){ return; }));

        if (justids) {
            var a = { property: ((type == 'files') ? 'id' : ((type == 'folders') ? 'name' : '')) };     // have to create a dummy object property...
            return_json[type].push({ a['property']: Content[type][index][a['property']] });
        } else {
            return_json[type].push(Content[type][index]);
        }           
    });

    return JSON.stringify(return_json);
},

ForEachItem: function(which = 0, onlyselected = true, method = function(index,type){}) {

    if (which == 0 || which == 1) {
        for (var i = 0; i < (onlyselected ? Content.selected.files.length : Content.files.length); i++) {
            method((onlyselected ? Content.selected.files[i] : i),1);
        }
    }

    if (which == 0 || which == 2) {
        for (var i = 0; i < (onlyselected ? Content.selected.folders.length : Content.folders.length); i++) {
            method((onlyselected ? Content.selected.folders[i] : i),2);
        }
    }
},
4

1 に答える 1

5

オブジェクト リテラル式では、構造の左側:は識別子または文字列定数のいずれかでなければなりません。それはあなたがやろうとしている式であってはなりません。

スタイル的に得たものと同じものを望む場合は、無名関数を使用してオブジェクトを構築できます。

  return_json[type].push(function() {
    var obj = {};
    obj[a.property] = Content[type][index][a.property];
    return obj;
  }());
于 2013-04-19T22:58:57.217 に答える