我正在使用 Windows Azure 移动服务为我的应用程序构建后端。对于服务器脚本的读取操作,现在我想检索脚本中的 $filter、$select 等查询参数。知道吗?
问问题
356 次
1 回答
1
在“读取”函数的参数中使用“查询”对象(通过使用 console.log )进行修改后,我终于找到了解决方案:
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 协议的任何内容
于 2012-11-22T10:56:08.417 回答