我真的对underscore mixin的数据查询underscore-data感兴趣。对于我当前正在从事的项目而言,这将是完美的。但是要么我无法使其正常工作,要么不了解如何使用RQL implementation工具。
pc = {
columns: [
{
title: "3column-left",
text: "text",
image: null
}
]
}
我想测试/ columns和/ columns / text是否存在。但是,我无法让该库传递真实或错误的值。例如,这两个查询都返回原始的
pc
对象:_.query(pc, 'columns&columns/text')
_.query(pc, 'columns&columns/fubar')
尝试测试给定值时是同一回事。这两个查询都返回原始对象,尽管第二个查询不应返回任何内容。
_.query(pc, 'columns/title', '3column-left')
_.query(pc, 'columns/title', 'doesntExist')
任何见解均表示赞赏。
最佳答案
这里的问题是您将text
引用为columns
的属性,而不是columns
元素的属性之一(columns
是数组)
为了解决这个问题,您必须按照以下方式构造RQL:
_.query(pc, 'columns/0/title=3column-left'); // exists
_.query(pc, 'columns/0/title=doesntExist'); // doesn't exist
除了:您还可以使用正则表达式:
_.query(pc, 'columns/0/title=re:left'); // exists
_.query(pc, 'columns/0/title=re:right'); // doesn't exist
这种方法存在明显的局限性,即必须知道要查询的对象的数组索引。
此处提供了基本信息:https://github.com/dvv/underscore-data/blob/master/README-rql.md,不幸的是,没有提到任何数组访问。
更新:
值得注意的是,
_.query
函数中的第3个参数用于选项,因此使用方式如下:_.query(pc, 'columns/title', '3column-left')
不会产生您想要的效果。