Skip to content

Commit

Permalink
fix pagination order issue
Browse files Browse the repository at this point in the history
  • Loading branch information
yi committed Mar 11, 2014
1 parent f7dc911 commit 99b6142
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 88 deletions.
9 changes: 4 additions & 5 deletions lib/controllers/ticket.js

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

2 changes: 1 addition & 1 deletion lib/models/ticket.js

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ticketman",
"description": "A simple pull-based job/ticket system contians a centeral ticket dispatcher and distributed workers. This system is written in NodeJS, runing on MongoDB",
"version": "0.1.8",
"version": "0.1.9",
"homepage": "https://github.com/yi/node-ticket-manager",
"author": {
"name": "yi",
Expand Down
55 changes: 20 additions & 35 deletions public/js/jquery.mongoose-endless-scroll.js

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

68 changes: 34 additions & 34 deletions public_src/js/jquery.mongoose-endless-scroll.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

class MongooseEndlessScroll

DIRECTION_NEXT = "after"
DIRECTION_DOWN = "after"

DIRECTION_PREV = "before"
DIRECTION_UP = "before"

DEFAULTS =
itemsToKeep: null
Expand Down Expand Up @@ -47,47 +47,49 @@ class MongooseEndlessScroll
console.log "[jquery.mongoose-endless-scroll::options]"
console.dir options

scrollListener = =>
$(window).one "scroll", =>
if ($(window).scrollTop() >= $(document).height() - $(window).height() - @options.inflowPixels)
@fetchNext()
else if $(window).scrollTop() <= @options.inflowPixels
@fetchPrev()
setTimeout scrollListener, @options.intervalFrequency
#scrollListener = =>
#$(window).one "scroll", =>
#if ($(window).scrollTop() >= $(document).height() - $(window).height() - @options.inflowPixels)
#@fetchNext()
#else if $(window).scrollTop() <= @options.inflowPixels
#@fetchPrev()
#setTimeout scrollListener, @options.intervalFrequency

$(document).ready =>
scrollListener()
#scrollListener()
if @options.autoStart then @fetchNext()

return

fetchNext : ->
#console.log "[jquery.mongoose-endless-scroll::fetchNext] @options.inflowPixels:#{@options.inflowPixels}"
$(window).scrollTop($(document).height() - $(window).height() - @options.inflowPixels)
#$(window).scrollTop($(document).height() - $(window).height() - @options.inflowPixels)

data = {}
data[DIRECTION_NEXT] = @ids[@ids.length - 1]
data[DIRECTION_DOWN] = @ids[@ids.length - 1]
@fetch data
return

fetchPrev: ->
#console.log "[jquery.mongoose-endless-scroll::fetchPrev] "
$(window).scrollTop(@options.inflowPixels)
#$(window).scrollTop(@options.inflowPixels)

data = {}
data[DIRECTION_PREV] = @ids[0]
data[DIRECTION_UP] = @ids[0]
@fetch data
return

fetch : (data)->
console.log "[jquery.mongoose-endless-scroll::fetch] "
console.dir data
#console.log "[jquery.mongoose-endless-scroll::fetch] "
#console.dir data

if @isFecthing
console.log "[jquery.mongoose-endless-scroll::fetch] in fetching"
return

if (data[DIRECTION_NEXT] is @downmonstId) || (data[DIRECTION_PREV] is @upmonstId)
debugger

if (data[DIRECTION_DOWN] is @downmonstId) || (data[DIRECTION_UP] is @upmonstId)
console.log "[jquery.mongoose-endless-scroll::fetch] reach boundary"
return

Expand All @@ -107,14 +109,11 @@ class MongooseEndlessScroll
@isFecthing = false

# figure out direction
currentDirection = if data.after? then DIRECTION_NEXT else DIRECTION_PREV
currentDirection = if data[DIRECTION_DOWN]? then DIRECTION_DOWN else DIRECTION_UP
#console.log "[jquery.mongoose-endless-scroll::receive] currentDirection:#{currentDirection}"

# FIXME:
# for some reason, the mongoose will always return results even before/after reaches the end
#
# following is quick fix to remove duplicate results
# ty 2014-03-11
debugger

res.results or= []
pos = 0
while pos < res.results.length
Expand All @@ -128,18 +127,18 @@ class MongooseEndlessScroll
# validate result
unless Array.isArray(res.results) and res.results.length
# reach boundary
if currentDirection is DIRECTION_NEXT
@downmonstId = data.after
@elLoadingNext.hide()
if currentDirection is DIRECTION_DOWN
@downmonstId = data[DIRECTION_DOWN]
#@elLoadingNext.hide()
else
@upmonstId = data.before
@elLoadingPrev.hide()
@upmonstId = data[DIRECTION_UP]
#@elLoadingPrev.hide()
return

@addInResults(res.results, currentDirection)

# render partial
if currentDirection is DIRECTION_NEXT then @renderBottomPartial() else @renderTopPartial()
if currentDirection is DIRECTION_DOWN then @renderBottomPartial() else @renderTopPartial()

# clear redundancy
if @options.itemsToKeep > 0 and (diff = @ids.length - @options.itemsToKeep) > 0
Expand All @@ -162,26 +161,27 @@ class MongooseEndlessScroll
#debugger
while(count > 0)
# remove on the opposite side
id = if direction is DIRECTION_NEXT then @ids.shift() else @ids.pop()
id = if direction is DIRECTION_DOWN then @ids.shift() else @ids.pop()

delete @idToData[id]
$("##{id}").remove()
--count

# show the more handler
if direction is DIRECTION_NEXT
@elLoadingPrev.show()
if direction is DIRECTION_DOWN
#@elLoadingPrev.show()
@topmostId = null
else
@elLoadingNext.show()
#@elLoadingNext.show()
@bottomostId = null
return

addInResults : (results, direction)->
results.reverse() if direction is DIRECTION_UP
for result in results
id = result._id
continue if ~@ids.indexOf(id)
if direction is DIRECTION_NEXT
if direction is DIRECTION_DOWN
@ids.push id
else
@ids.unshift id
Expand Down
15 changes: 4 additions & 11 deletions src/controllers/ticket.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,17 @@ exports.index = (req, res, next)->
res.render 'tickets/index',
title: 'All Tickets'
tickets : []

#Ticket.find().sort({created_at:'desc'}).exec (err, tickets)->
#return next err if err?
#res.render 'tickets/index',
#title: 'All Tickets'
#tickets : tickets
#return
return


exports.list = (req, res, next)->
debuglog "list "

console.dir req.query
debuglog "list req.query: %j", req.query

Ticket.paginate(req.query || {}, '_id').sort({created_at:'desc'}).execPagination (err, result)->
Ticket.paginate(req.query || {}, '_id').select('-comments -content').execPagination (err, result)->
return next err if err?
result.success = true
console.log "[ticket::list] dump result:"
console.dir result
res.json result
return

Expand Down
2 changes: 1 addition & 1 deletion src/models/ticket.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ TicketSchema.plugin timestamps,
TicketSchema.plugin paginator,
limit: 10,
defaultKey: '_id',
direction: 1
direction: 'desc'

## Validations
TicketSchema.path('title').validate (val)->
Expand Down

0 comments on commit 99b6142

Please sign in to comment.