Backbone集合从json获取特定数据

时间:2013-06-05 06:26:52

标签: json backbone.js

嗨,我有以下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"}
]

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

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

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

3 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:0)

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

 myModel = collection.get(1);

其中集合应该是您的集合实例

答案 2 :(得分:0)

如果您已经拥有所有json数据,并希望将其过滤到一个匹配的记录,则可以使用findWhere

myModel = collection.findWhere({id: 1});