Created
August 12, 2015 05:08
-
-
Save JohannesFischer/7e7c7eb7cb04af924386 to your computer and use it in GitHub Desktop.
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
## 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 |
Author
JohannesFischer
commented
Aug 12, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment