How do I check specific data when reading in data from a remote source with kendo.data.datasource

362 views Asked by At

I am using Telerik Platform to build a hybrid news app. I am pulling in data remotely and I have everything working correctly, but now I need to check to see for featured stories and move them into an variable. I have the data pulling into like this:

viewModel = new kendo.observable({
            newsItems: new kendo.data.DataSource({
                transport: {
                    read: {
                        url: serviceURL,
                        dataType: "jsonp"
                    }
                },
                schema: {
                    data: "posts"
                }
            }) 
});

When the json is pulled it looks like this when I console.log(viewModel):

ht.exend.init
    _aggregate:
    _changeHandler:
    _data: ht.extend.init[25]
        0: ht.extend.init    //Each article
            article_date: "June 09, 2015"
            author: "James Dean"
            categories: ht.extend.init[1]
                0: ht.extend.init
                    id: 1
                    title: "OC"
                length: 1
            content: "<p>Content</p>"
            custom_fields: ht.extend.init
                Views:
                featured_post: ht.extend.init[1]
                    0: "0"     //0 if not featured, 1 if featured
                    length: 1
                parent: function (){return i}
            date: "2015-06-09"
            .
            .
            .
            url: "url"
        1: ht.extend.init
        2: ht.extend.init
        .
        .
        .
        25: ht.extend.init
    _destroyed: Array[0]
    .
    .
    .
    transport: ft.extend.init

What I would like to do is have it check each article to see if it is a featured post (ie have the value of 1) and move it to an array of var featured = []. I cannot find anything in the telerik documentation, and I have tried to put success: function () {} in the call for the dataSource but that doesn't work either. Thank you in advance.

1

There are 1 answers

3
Rick S On BEST ANSWER

You should be able to get access to your json data within the datasource. I'm using the requestEnd event. You didn't explain exactly what you're trying to do with the data once you know an article is featured so I'm just showing a basic way to get the data and work with it after it's retrieved.

viewModel = new kendo.observable({
        newsItems: new kendo.data.DataSource({
            transport: {
                read: {
                    url: serviceURL,
                    dataType: "jsonp"
                }
            },
            schema: {
                data: "posts"
            },
                requestEnd: function (e) {
                    var response = e.response;
                    var type = e.type;
                    var obj = JSON.parse(response);
                    //now use obj to access your json data
                    //The if statement is just an example, you will need to
                    //change it to fit your data model 
                    if ( obj.featured_post == 1 ) {
                        //do something
                    }
                }
        }) 
});