Created
November 27, 2014 09:22
-
-
Save shengoo/882ae66db021f3e61c73 to your computer and use it in GitHub Desktop.
Kendo UI DataSource work with angular js service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module("app", ["kendo.directives"]) | |
.controller("news", function($scope,newsService) { | |
var dataSource = new kendo.data.DataSource({ | |
transport: { | |
read: dataSourceRead | |
} | |
}); | |
function dataSourceRead(options){ | |
//todo: show loading | |
newsService.getByCategory($scope.selectedCategory.value) | |
.then( | |
function(response){ | |
options.success(response); | |
//todo: hide loading | |
}, | |
function(response){ | |
options.error(response); | |
//todo: handle errors. | |
}); | |
} | |
$scope.newsListViewOptions = { | |
dataSource: dataSource | |
}; | |
}) | |
.service('newsService', function($q, $http) { | |
this.getByCategory = function(category){ | |
var url = "your url"; | |
var request = $http({ | |
method: "jsonp", | |
url: url | |
}); | |
return( request.then( handleSuccess, handleError ) ); | |
}; | |
function handleError( response ) { | |
//if no message return from server | |
if ( | |
! angular.isObject( response.data ) || | |
! response.data.message | |
) { | |
return( $q.reject( "An unknown error occurred." ) ); | |
} | |
return( $q.reject( response.data.message ) ); | |
} | |
function handleSuccess( response ) { | |
return( response.data ); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment