Sha256: e27520b0a3b8dfbda5c693ed829fb7ead58fe3c31a3a9cddc97adabd15f355e7

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

class Reader
  constructor: (options) ->
    @$ = jQuery

    @id = options.id
    @key = options.key
    @token = null
    @collection = []
    @position = 0

  next: (count) ->
    deferred = @$.Deferred()

    nextPosition = @position + count

    if nextPosition <= @collection.length
      elements = @collection.slice(@position, nextPosition)
      @position = nextPosition
      deferred.resolve(elements)

    else
      @load()
        .done =>
          nextPosition = Math.min(nextPosition, @collection.length)
          elements = @collection.slice(@position, nextPosition)
          @position = nextPosition
          deferred.resolve(elements)
          return

        .fail (details...) =>
          deferred.reject(details...)
          return

    deferred.promise()

  load: ->
    url = "https://www.googleapis.com/plus/v1/people/#{@id}/activities/public?key=#{@key}"
    url = "#{url}&pageToken=#{@token}" if @token

    @$.ajax(url: url, crossDomain: true, dataType: 'jsonp').done (result) =>
      @append(result.items)
      @token = result.nextPageToken
      return

  append: (items) ->
    @collection.push(item) for item in items
    return

window.GooglePlus ||= {}
window.GooglePlus.Reader = Reader

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
googleplus-reader-0.0.4 lib/assets/javascripts/googleplus.reader.js.coffee