Get pagination options form the view

This commit is contained in:
Alfredo Sumaran 2016-04-01 12:55:45 -05:00
parent f3134c2a70
commit 8c0aba9458
3 changed files with 19 additions and 10 deletions

View file

@ -1,6 +1,11 @@
class @Todos
constructor: (@name) ->
@todos_per_page = gon.todos_per_page || 20
constructor: (opts = {}) ->
{
@el = $('.js-todos-options')
} = opts
@perPage = @el.data('perPage')
@clearListeners()
@initBtnListeners()
@ -59,26 +64,30 @@ class @Todos
$('.todos-pending .badge, .todos-pending-count').text data.count
$('.todos-done .badge').text data.done_count
getRenderedPages: ->
$('.gl-pagination .page').length
getTotalPages: ->
@el.data('totalPages')
getCurrentPage: ->
parseInt($.trim($('.gl-pagination .page.active').text()))
@el.data('currentPage')
getTodosPerPage: ->
@el.data('perPage')
redirectIfNeeded: (total) ->
currPages = @getRenderedPages()
currPages = @getTotalPages()
currPage = @getCurrentPage()
newPages = Math.ceil(total / @todos_per_page)
newPages = Math.ceil(total / @getTodosPerPage())
url = location.href # Includes query strings
# Refresh if no remaining Todos
if !total
if not total
location.reload()
return
# Do nothing if no pagination
return if !currPages
return if not currPages
# If new total of pages is different than we have now
if newPages isnt currPages

View file

@ -2,7 +2,6 @@ class Dashboard::TodosController < Dashboard::ApplicationController
before_action :find_todos, only: [:index, :destroy, :destroy_all]
def index
gon.todos_per_page = Todo.default_per_page
@todos = @todos.page(params[:page])
end

View file

@ -45,6 +45,7 @@
.prepend-top-default
- if @todos.any?
.js-todos-options{ data: {per_page: @todos.limit_value, current_page: @todos.current_page, total_pages: @todos.total_pages} }
- @todos.group_by(&:project).each do |group|
.panel.panel-default.panel-small.js-todos-list
- project = group[0]