Skip to content

Instantly share code, notes, and snippets.

@JohannesFischer
Created August 12, 2015 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohannesFischer/7e7c7eb7cb04af924386 to your computer and use it in GitHub Desktop.
Save JohannesFischer/7e7c7eb7cb04af924386 to your computer and use it in GitHub Desktop.
## Data Service
myApp.provider 'dataService', ->
data =
content: []
currentIndex: -1
error: false
filter: 'list'
infiniteBusy: false
loading: false
postType: ''
@.$get = [
'myFactory',
'$log',
'$timeout',
'env',
(myFactory, $log, $timeout, env) ->
data: data
add: (item) ->
data.content.push(item)
clear: ->
data.content = []
data.infiniteBusy = false
data.loading = false
someSharedFunction: ->
return false
get: (url) ->
# disable infinte-scroll
data.infiniteBusy = true
data.loading = true
# add URL params
if url.indexOf('max_time') is -1
url += if url.indexOf('?') is -1 then '?' else '&'
url += 'offset=' + data.content.length
# check for limit parameter
limitRegExp = new RegExp(/\?limit=([a-z0-9\-]+)\&?/i)
limit = limitRegExp.exec(url)
if limit then limit = parseInt(limit[1]) else null
myFactory.get(url).then (response) ->
if response.length > 0
for i in [0..response.length-1]
data.content.push response[i]
if limit?
$timeout(( ->
data.infiniteBusy = limit > response.length
), 500
)
else
$timeout(( ->
data.infiniteBusy = false
), 500
)
# hide loader
data.loading = false
(errorPayload) ->
data.error = true
$log.error 'failure loading content', errorPayload
undefined
getData: ->
@clear()
@data
]
undefined
@JohannesFischer
Copy link
Author

# Controller
myApp.controller 'myController', [
  '$scope',
  'dataService',
($scope, dataService) ->
  $scope.data = dataService.data
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment