kendo.data.DataSource transport.Read() method is calling 2 times

2 Answers 33 Views
Grid
Pratyush
Top achievements
Rank 1
Pratyush asked on 02 Jan 2024, 05:08 AM

so I have JS code . In UI I have two radio buttons like communities  and badges . When I clicked on Badges then kendo.data.DataSource transport.Read() method is calling two time but read method should call one time . how can I stop read method for multiple calling. Here I am sharing my code . Can you modify this code by which we can call read method at one time not multiple time

getData = function () {
            $scope.searchDS = [];
            var count = 1;
            if ($scope.$root.usrID != -1) {
                $scope.searchDS = new kendo.data.DataSource({

                    transport: {
                        read: {
                            url: function () {
                                return getQueryUrl();
                            },
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            dataType: 'json',
                            beforeSend: function (e) {
                                e.preventDefault();
                            }
                        },
                        parameterMap: function (options, operation) {
                            // $scope.badgeItemsCnt = 0;
                            console.log(operation);
                            console.log(options);
                            var sort = "";
                            if ($scope.frm.sortBy.Value == "1") //A-Z Order asc
                                sort = { "badgeTitle.raw_lowercase": { "order": "asc" } };
                            else if ($scope.frm.sortBy.Value == "2") //Z-A Order desc //
                                sort = { "badgeTitle.raw_lowercase": { "order": "desc" } };
                            else if ($scope.frm.sortBy.Value == "3") //Newest First desc
                                sort = { "CreatedDate": { "order": "desc" } };
                            else if ($scope.frm.sortBy.Value == "4") //Oldest First asc
                                sort = { "CreatedDate": { "order": "asc" } };

                            var filter = getFacetFilters($scope.frm.sortBy.Value);

                            var query = { "query_string": { "query": getSearchFilter() } };

                            var shouldfilter = [];
                            var mustNotFilter = [];
 
                            if ($scope.$root.usrID > -1) {
                                if ($scope.badgeSearchType == "Public") {
                                    mustNotFilter.push({ "term": { "publishStatus.raw": "Private" } });
                                }
                                //else if ($scope.badgeSearchType == "Private") {
                                //        shouldfilter.push({ "term": { "ownerId.raw": appSettings.userProfileID } });
                                //        filter.push({ "term": { "publishStatus.raw": "Private" } });
                                //    }
                            }

                            if ($scope.$root.usrID > -1) {
                                shouldfilter.push({ "term": { "userProfileID.raw": $scope.$root.usrID } });
                                //if ($scope.$root.profile_cid > -1) {
                                //    filter.push({ "term": { "communityID": parseInt($scope.$root.profile_cid) } });
                                //}
                            }
                            else if (appSettings.examTakerID > -1) {
                                shouldfilter.push({ "term": { "examTakerID.raw": appSettings.examTakerID } });
                            }
                            query = { "must": query, "filter": { "bool": { "minimum_should_match": shouldfilter.length > 0 ? 1 : 0, "should": shouldfilter, "must": filter, "must_not": mustNotFilter } } }

                            $scope.GetQuery = query;

                            var str = JSON.stringify({
                                //"_source": true,
                                //"_source": {
                                //    "excludes": ["communityName"]
                                //},
                                "sort": sort,
                                "query": { "bool": query },
                                "aggs": JSON.parse("{" + getFacets($scope.frm.sortBy.Value) + "}")
                            });

                            return str;

                        }
                    },
                    serverPaging: true,

                    pageSize: 10,
                    page: 1,
                    schema: {
                        type: "json",
                        data: function (data) {
                            bindFacets(data.aggregations);
                            return data.hits.hits;

                        },
                        total: function (data) {
                            $scope.badgeItemsCnt = data.hits.total.value;
                            if ($scope.badgeItemsCnt == 0) {
                                $scope.showNobadges = true;
                            }
                            return data.hits.total.value;
                        }
                    }
                });
                try {
                    MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
                } catch (e) { }

            }
        }


Martin
Telerik team
commented on 04 Jan 2024, 02:02 PM

Hello, Pratyush,

Would it be possible to provide a runnable example where we can observe and test the behaviour? Thank you in advance for the cooperation.

2 Answers, 1 is accepted

Sort by
0
Trusha
Top achievements
Rank 1
Iron
answered on 04 Apr 2024, 11:21 AM

Hello, Pratyush

You can try to off the click events of buttons before on.

For example:

$("#btnBadges").off("click").on("click", function(){

// YOUR CODE

});

0
Trusha
Top achievements
Rank 1
Iron
answered on 04 Apr 2024, 11:21 AM

Hello Pratyush,

You can try to off the click event of your buttons before on.

For example:

$("#btnBadges").off("click").on("click", function(){

// YOUR CODE 

});


Tags
Grid
Asked by
Pratyush
Top achievements
Rank 1
Answers by
Trusha
Top achievements
Rank 1
Iron
Share this question
or