如何在Backbone中过滤此JSON?

时间:2013-06-26 04:54:51

标签: backbone.js

我将来自两个单独的mongodb模式的数据填充到下面的对象中: 他们是用户和文章。我如何过滤/排序,以便我可以通过骨干中的user._id找到文章? 这适用于单页博客类型的网站,其中每个用户都有自己的文章。

我在下划线中查看_.filter和_.where函数,但我仍然是新手。任何帮助表示赞赏。感谢。

以下是我的服务器上我正在尝试构建的示例(使用嵌入式架构完成): http://kevg.co:3700/demo1 和骨干模型/视图/集合代码在这里:  http://kevg.co:3700/javascripts/demoj17.js

//Individual Model
  {
    "user": {
      "username": "ho",
      "email": "hom@gmail.com",
      "_id": "51be709a148846ec25000007"
    },
    "name": "money",
    "articlebody": "",
    "_id": "51c1033283376a5808000002",
    "__v": 0,
    "createdAt": "2013-06-19T01:02:42.424Z"
  },

//Rest of data

  {
    "user": {
      "username": "kev",
      "email": "kevo@o.com",
      "_id": "51be6fe9148846ec25000001"
    },
    "name": "bob",
    "articlebody": "",
    "_id": "51c89ab47596ef1018000001",
    "__v": 0,
    "createdAt": "2013-06-24T19:15:00.835Z"
  },
  {
    "user": {
      "username": "kev",
      "email": "kevo@o.com",
      "_id": "51be6fe9148846ec25000001"
    },
    "name": "sasa",
    "articlebody": "sajdja",
    "_id": "51c8a3bf341eb4141f000001",
    "__v": 0,
    "createdAt": "2013-06-24T19:53:35.233Z"
  }

2 个答案:

答案 0 :(得分:2)

我有一组用户以及一系列文章。

要获取特定用户发布的文章集合,您可以执行类似于以下内容的操作:

// theUser would be the user selected
articlesCollection.where({'User': theUser});

谢谢,Loamhoof指出减少。

请注意,您的用户模型还可以包含用户发布的文章集合。

答案 1 :(得分:1)

假设您的数据如下所示

var data = [{
    "user": {
        "username": "ho",
            "email": "hom@gmail.com",
            "_id": "51be709a148846ec25000007"
    },
        "name": "money",
        "articlebody": "",
        "_id": "51c1033283376a5808000002",
        "__v": 0,
        "createdAt": "2013-06-19T01:02:42.424Z"
}, {
    "user": {
        "username": "kev",
            "email": "kevo@o.com",
            "_id": "51be6fe9148846ec25000001"
    },
        "name": "bob",
        "articlebody": "",
        "_id": "51c89ab47596ef1018000001",
        "__v": 0,
        "createdAt": "2013-06-24T19:15:00.835Z"
}, {
    "user": {
        "username": "kev",
            "email": "kevo@o.com",
            "_id": "51be6fe9148846ec25000001"
    },
        "name": "sasa",
        "articlebody": "sajdja",
        "_id": "51c8a3bf341eb4141f000001",
        "__v": 0,
        "createdAt": "2013-06-24T19:53:35.233Z"
}];

您可以使用Array中的过滤功能或下划线中的过滤功能来过滤数据

function getArticles(userName) {
    return _.filter(data, function (item) {
        return item.user.username == userName; // give the property to be used for filtering
    });
}
var kevArticles = getArticles("kev");
var hoArticles = getArticles("ho");

或者,您可以使用下划线中的groupBy对与用户相关的数据进行分组

function groupByUserName(){
    return _.groupBy(data, function (item) {
        return item.user.username; // give the property to be used for filtering
    });
}

 var groupedData = groupByUserName();

这将使其采用下面给出的格式

{
    "ho": [{
        "user": {
            "username": "ho",
            "email": "hom@gmail.com",
            "_id": "51be709a148846ec25000007"
        },
        "name": "money",
        "articlebody": "",
        "_id": "51c1033283376a5808000002",
        "__v": 0,
        "createdAt": "2013-06-19T01:02:42.424Z"
    }],
    "kev": [{
        "user": {
            "username": "kev",
            "email": "kevo@o.com",
            "_id": "51be6fe9148846ec25000001"
        },
        "name": "bob",
        "articlebody": "",
        "_id": "51c89ab47596ef1018000001",
        "__v": 0,
        "createdAt": "2013-06-24T19:15:00.835Z"
    }, {
        "user": {
            "username": "kev",
            "email": "kevo@o.com",
            "_id": "51be6fe9148846ec25000001"
        },
        "name": "sasa",
        "articlebody": "sajdja",
        "_id": "51c8a3bf341eb4141f000001",
        "__v": 0,
        "createdAt": "2013-06-24T19:53:35.233Z"
    }]