0

我正在使用backbone.js。我正在尝试向 restful 服务发送请求,我将结果集作为 json 对象获取,如图所示

{
"Msgs": [
    "Alert",
    "Not"
],
"MessageStatus": [
    "Active",
    "Inactive"
],
"date": {
    "From": "2013-04-25",
    "To": "2013-06-25"
},
"Mlist": {
    "Status": "PND",
    "Role": "Admin,User",
    "To": "2013-06-24",
    "Id": 6,
    "Datecreated": "2013-06-24",
    "Title": "Title5",
    "From": "2013-06-20"
}
}.

我正在提取 json 对象并将其设置为集合,但我无法从具有特定 ID 的集合中获取特定模型。

4

2 回答 2

0

I think you need to set your model and parse your input data. Checkout http://backbonejs.org/#Collection-model, http://backbonejs.org/#Model-parse and http://backbonejs.org/#Collection-parse.

于 2013-08-30T10:24:27.883 回答
0

如果你想让你的模型可以用 id 来识别。您必须在属性哈希中设置 id:

{
  "id": 1001,
  "Msgs": [
    "Alert",
    "Not"
  ],
  "MessageStatus": [
    "Active",
    "Inactive"
  ],
  "date": {
    "From": "2013-04-25",
    "To": "2013-06-25"
  },
  "Mlist": {
    "Status": "PND",
    "Role": "Admin,User",
    "To": "2013-06-24",
    "Id": 6,
    "Datecreated": "2013-06-24",
    "Title": "Title5",
    "From": "2013-06-20"
   }
}

然后使用主干集合“findWhere”方法来获取具体的id模型。

//assume msgCollection is which you put the models
var model = msgCollection.findWhere({id: 1001});  

希望这对你有帮助。

于 2013-06-25T16:54:38.837 回答