I'm learning Angular and I don't like my current approach. The current
controller uses first `Geolocation` service and when `getLocation()` is
resolved, then the `Pagination` service is invoked with the location param.
Is it any better solution to provide one service that query server with
location? Can I integrate current logic and move or use the `Pagination`
service directly through Post factory, and query server with params of
given page and location once resolved? I would like to load new data
through invoke just one resource method e.g. from scope loadPage()
function. How can I refactor this code?
app.controller('MainCtrl',['$scope','Post','Pagination','Geolocation'
($scope, Post, Pagination, Geolocation) ->
$scope.posts = []
Geolocation.getLocation().then (location) ->
Pagination.paginate $scope, Post, 10, (postsRes) ->
$scope.posts.push posts for posts in postsRes
, params: {location: location}
])
app.factory 'Post', ($resource) ->
$resource '/posts/:id.json', { id: '@id' }
app.service "Geolocation", ['$q', '$window', ($q, $window) ->
@getLocation = ->
deferred = $q.defer()
return deferred.resolve @loc if @loc
if $window.navigator and $window.navigator.geolocation
$window.navigator.geolocation.getCurrentPosition (position) ->
@loc = position.coords
deferred.resolve position.coords
deferred.promise
]
app.service 'Pagination', ->
@paginate = ($scope, @resource, @maxElemPerPage=10, @onNewPage, @query={})
->
@page = 1
@canLoadNextPage = true
$scope.loadPage = (promise) =>
return promise.resolve() unless @canLoadNextPage
@load promise
@load = (promise) =>
@canLoadNextPage = false
q = angular.extend @query, page: @page
@resource.query q, (resources) =>
@canLoadNextPage = true if resources.length is @maxElemPerPage
@page++
@onNewPage resources if @onNewPage
promise.resolve() if promise
@load()
@
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.