Sha256: 11046f609fee52c954483e4b80581fbbc954a462f3ac80da5c3c3debe82d52de

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

class TentStatus.Views.FetchPostsPool extends Backbone.View
  initialize: (options = {}) ->
    @parentView = options.parentView

    @$numNewPosts = ($ '.num_new_posts', @$el)
    @numNewPosts = 0

    @$postsList = ($ 'ul.posts', @parentView.container.$el)

    @sinceId = @parentView.posts.first()?.get('id')
    @pool = new TentStatus.FetchPool( new TentStatus.Collections.Posts, { sinceId: @sinceId })
    @pool.on 'fetch:success', @update

    @fetchDelay = 3000
    @fetchDelayOffset = 0

    @setFetchInterval()

    @$el.on 'click', (e)=>
      e.preventDefault()
      @emptyPool()
      false
    
  setFetchInterval: (interval=@fetchDelay+@fetchDelayOffset) =>
    clearInterval TentStatus._fetchPostsPoolInterval
    TentStatus._fetchPostsPoolInterval = setInterval @pool.fetch, interval

  update: =>
    lastSinceId = @sinceId
    @sinceId = @pool.sinceId
    if lastSinceId == @sinceId
      @fetchDelayOffset = Math.min(@fetchDelayOffset + @fetchDelay, 57000) # max delay: 1 min
      @setFetchInterval()
    else
      @fetchDelayOffset = 0
      @setFetchInterval()

    @numNewPosts = @pool.collection.length
    @$numNewPosts.text @numNewPosts
    @show() if @numNewPosts > 0

  show: => @$el.show()
  hide: => @$el.hide()

  emptyPool: =>
    for i in [0...@numNewPosts]
      post = @pool.collection.shift()
      TentStatus.Collections.posts.unshift(post)
      # @createPostView(post)
    @parentView.render()
    @pool.sinceId = @parentView.posts.first()?.get('id') || @pool.sinceId
    @numNewPosts = 0
    @$numNewPosts.text @numNewPosts
    @hide()

  createPostView: (post) =>
    el = ($ '<li>').prependTo(@$postsList)
    view = new TentStatus.Views.Post el: el, parentView: @parentView
    view.post = post
    context = view.context(post)
    view.render(context)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tent-status-0.0.1 assets/javascripts/views/fetch_posts_pool.js.coffee