-
-
Save 5inline/db366cb5d570ce8e2d86 to your computer and use it in GitHub Desktop.
Example of directive to handle famous-angular scroll-view events
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
// Directive | |
angular.module('[app]') | |
.directive('faScrollEvents', function ($parse, $famous) { | |
return { | |
restrict: 'A', | |
link : function ($scope, $elem, $attr) | |
{ | |
$scope.$watch($attr.faScrollEvents, function (newVal, oldVal) | |
{ | |
if( !newVal ) return; | |
var _scope = $elem.scope(), | |
_isolate = $famous.getIsolate(_scope), | |
_node = _isolate.renderNode, | |
_sync = _isolate.renderNode.sync | |
; | |
$scope.$emit('scrollViewReady',_node,_sync); | |
if( newVal.onStart ) { | |
_sync.on('start', function (data) { | |
newVal.onStart(data, _node, _sync, _scope, _isolate) | |
}); | |
} | |
if( newVal.onUpdate ) { | |
_sync.on('update', function (data) { | |
newVal.onUpdate(data, _node, _sync, _scope, _isolate) | |
}); | |
} | |
if( newVal.onEnd ) { | |
_sync.on('end', function (data) { | |
newVal.onEnd(data, _node, _sync, _scope, _isolate); | |
}); | |
} | |
if( newVal.onSettle ) { | |
_node.on('settle', function (data) { | |
newVal.onSettle(data, _node, _sync, _scope, _isolate); | |
}); | |
} | |
if( newVal.onEdge ) { | |
_node.on('onEdge', function (data) { | |
newVal.onEdge(data, _node, _sync, _scope, _isolate); | |
}); | |
} | |
if( newVal.offEdge ) { | |
_node.on('offEdge', function (data) { | |
newVal.offEdge(data, _node, _sync, _scope, _isolate); | |
}); | |
} | |
}); | |
} | |
}; | |
}); | |
// In your controller | |
$scope.scrollEvents = { | |
currentPosition : 0, | |
refreshThreshold : -40, | |
watchRefresh : false, | |
onEdge : function (data, node, sync) | |
{ | |
if( node.getAbsolutePosition() <= 0 || loading ) return; | |
// Bottom of list, load more items | |
}, | |
onUpdate : function (data, node, sync) | |
{ | |
if( node.getAbsolutePosition() < -10 ) { | |
// Overscroll, like pull to refresh | |
this.watchRefresh = true; | |
} | |
}, | |
onEnd : function (data, node, sync) | |
{ | |
if( !this.watchRefresh || loading ) return; | |
if( node.getAbsolutePosition() <= this.refreshThreshold ) { | |
// Refresh items | |
} else { | |
} | |
this.watchRefresh = false; | |
} | |
}; | |
// In your HTML | |
<fa-scroll-view fa-scroll-events="scrollEvents"></fa-scroll-view> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment