0

嗨,我有以下 JSON:

[
    {"id":"1","question":"Whats is your name ?","type":"Text","source":"-","default":"-","control":"-","requestDate":"Yes","required":"Yes"},
    {"id":"2","question":"What is your Age ?","type":"Text","source":"-","default":"-","control":"-","requestDate":"No","required":"No"},
    {"id":"3","question":"What is your interest?","type":"Text","source":"-","default":"-","control":"-","requestDate":"Yes","required":"No"},
    {"id":"4","question":"What is your qualification?","type":"Text","source":"-","default":"-","control":"-","requestDate":"Yes","required":"Yes"},
    {"id":"5","question":"What is your Nic ?","type":"Text","source":"-","default":"-","control":"-","requestDate":"Yes","required":"No"}
]

现在我必须从 json 中获取数据,其 id 在获取期间提供,我想通过 id 参数获取数据。例如,获取结果提供该 json 对象

{"id":"1","question":"Whats is your name ?","type":"Text","source":"-","default":"-","control":"-","requestDate":"Yes","required":"Yes"}

如果我在 collection.fetch 事件中提供 id=1 .. 任何方法?

4

3 回答 3

2

如果我正确理解您的问题,您正在寻找:

collection.fetch({ data: $.param({ id: 1}) });

您还必须添加一些代码服务器端。

于 2013-06-05T06:56:01.080 回答
0

如果您已经拥有所有 json 数据,并且想要将其过滤为仅匹配的一条记录,则可以使用findWhere

myModel = collection.findWhere({id: 1});
于 2013-06-05T17:09:59.597 回答
0

如果您在 Backbone Collection 中有该 JSON。您可以通过传入 id、cid 或 Model 对象来获取特定的模型。在你的情况下

 myModel = collection.get(1);

集合应该是你的集合实例是什么

于 2013-06-05T06:47:08.627 回答