Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify one attribute of a model inside a collection?

render: function () {
    news.fetchMyNews();

    for (var i = 1; i <= news.length; i++) {
        var newsData = news.get(i);
        var newsRow = JST["news/row"](newsData.attributes);
        $("#news_tbody").append(newsRow);
        if (newsData.is_read == 1) {
            this.$('tr').attr("class", "news_read");
        } else if (newsData.is_read == 0) {
            this.$('tr').attr("class", "news_unread");
        }
    }
}

In this code newsData.attributes is retrieved well and I get the table with 3 rows rendered.

However, the newsData.is_read values are not retrieved and there is no error message at all, thus, the rows don't get styling.

news is a collection.

I wonder, what can be wrong with this? JSON file that I'm using for testing looks like this:

[{
    "id": 1,
    "_type": "friends",
    "message": "Your friend ...",
    "is_read": 1
},

{
    "id": 2,
    "_type": "friends",
    "message": "Your friend ...",
    "is_read": 0
},

{
    "id": 3,
    "_type": "other",
    "message": "User ...",
    "is_read": 1
}]
like image 209
Arman Ospanov Avatar asked Nov 12 '22 17:11

Arman Ospanov


1 Answers

Since newsData is a Model for getting it's attributes either .get('is_read') or newsData.attributes.is_read should be used.

like image 93
undefined Avatar answered Nov 15 '22 02:11

undefined