如何在服务器脚本中检索查询参数

时间:2012-11-22 06:50:17

标签: mobile azure

我正在使用Windows Azure移动服务为我的应用构建后端。对于服务器脚本的读取操作,现在我想检索查询参数,如$ filter,脚本中的$ select等。有什么想法?

1 个答案:

答案 0 :(得分:1)

在'read'函数的参数(通过使用console.log)中查找'query'对象后,我终于找到了解决方案:

function isObject(variable) {
    return variable !== null && 
        variable !== undefined && 
        typeof variable === 'object';
}

// Find all the member-value pairs from the expression object
function findMemberValuePairsFromExpression (expr, ret) {
    if (!isObject(expr)) {
        return null;
    }
    ret = ret || {};
    for (var name in expr) {
        if (expr.hasOwnProperty(name)) {
            var prop = expr[name];
            if (name === 'parent') { // Ignore parent property since it's added by us
                continue;
            }
            else if (name === 'left') { // member expression are in the left subtree
                if (isObject(prop)) {
                    prop.parent = expr; // Remember the parent
                    findMemberValuePairsFromExpression(prop, ret);
                }
            }
            else if (name === 'member') {
                // Found a member expression, find the value expression 
                // by the knowledge of the structure of the expression
                var value = expr.parent.right.value;
                ret[prop] = value;
            }
        }
    }

    if (expr.parent) {
        // Remove the added parent property
        delete expr.parent;
    }

    return ret;
}

// Get the filters component from query object and 
// find the member-value pairs in it
function findMemberValuePairsFromQuery (query) {
    var filters = query.getComponents().filters;
    return findMemberValuePairsFromExpression(filters);
}

function read (query, user, request) {
    request.execute();
}

请记住,这种方法严重依赖于查询对象的内部结构,因此将来可能会中断。

query.getComponents()还返回查询的其他部分,如“select”,“skip”,“top”等。基本上是oData协议的任何部分