Skip to content

Commit

Permalink
Add events. Closes CodeByZach#44
Browse files Browse the repository at this point in the history
  • Loading branch information
Zack Bloom committed Jan 13, 2014
1 parent aa316bf commit ac9c25d
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 3 deletions.
17 changes: 17 additions & 0 deletions docs/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@ whenever `pushState` or `replaceState` is called by default.

- `Pace.ignore`: Expliticly ignore one or more requests, see Tracking below

Events
------

Pace fires the following events:

- `start`: When pace is initially started, or as a part of a restart
- `stop`: When pace is manually stopped, or as a part of a restart
- `restart`: When pace is restarted (manually, or by a new AJAX request)
- `done`: When pace is finished
- `hide`: When the pace is hidden (can be later than `done`, based on `ghostTime` and `minTime`)

You can bind onto events using the `on`, `off` and `once` methods:

- `Pace.on(event, handler, [context])`: Call `handler` (optionally with context) when `event` is triggered
- `Pace.off(event, [handler])`: Unbind the provided `event` and `handler` combination.
- `Pace.once(event, handler, [context])`: Bind `handler` to the next (and only the next) incidence of `event`

Tracking
--------

Expand Down
44 changes: 44 additions & 0 deletions pace.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,45 @@ getFromDOM = (key='options', json=true) ->
catch e
console?.error "Error parsing inline pace options", e

class Evented
on: (event, handler, ctx, once=false) ->
@bindings ?= {}
@bindings[event] ?= []
@bindings[event].push {handler, ctx, once}

once: (event, handler, ctx) ->
@on(event, handler, ctx, true)

off: (event, handler) ->
return unless @bindings?[event]?

if not handler?
delete @bindings[event]
else
i = 0
while i < @bindings[event].length
if @bindings[event][i].handler is handler
@bindings[event].splice i, 1
else
i++

trigger: (event, args...) ->
if @bindings?[event]
i = 0
while i < @bindings[event].length
{handler, ctx, once} = @bindings[event][i]

handler.apply(ctx ? @, args)

if once
@bindings[event].splice i, 1
else
i++

window.Pace ?= {}

extend Pace, Evented::

options = Pace.options = extend defaultOptions, window.paceOptions, getFromDOM()

class NoTargetError extends Error
Expand Down Expand Up @@ -572,6 +609,7 @@ do init = ->
uniScaler = new Scaler

Pace.stop = ->
Pace.trigger 'stop'
Pace.running = false

bar.destroy()
Expand All @@ -586,6 +624,7 @@ Pace.stop = ->
init()

Pace.restart = ->
Pace.trigger 'restart'
Pace.stop()
Pace.start()

Expand Down Expand Up @@ -632,10 +671,14 @@ Pace.go = ->
if bar.done() or done or cancelAnimation
bar.update 100

Pace.trigger 'done'

setTimeout ->
bar.finish()

Pace.running = false

Pace.trigger 'hide'
, Math.max(options.ghostTime, Math.min(options.minTime, now() - start))
else
enqueueNextFrame()
Expand All @@ -653,6 +696,7 @@ Pace.start = (_options) ->
if not document.querySelector('.pace')
setTimeout Pace.start, 50
else
Pace.trigger 'start'
Pace.go()

if typeof define is 'function' and define.amd
Expand Down
81 changes: 79 additions & 2 deletions pace.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ac9c25d

Please sign in to comment.