Skip to content

Instantly share code, notes, and snippets.

@matedemorphy
Created July 1, 2020 23:57
Show Gist options
  • Save matedemorphy/94c6ad13b6aebd89862b5b14120c98df to your computer and use it in GitHub Desktop.
Save matedemorphy/94c6ad13b6aebd89862b5b14120c98df to your computer and use it in GitHub Desktop.
/views/shared/_paginator_nav.html.slim
nav
ul.pagination
li.page-item
a.page-link data-page="#{pagy.prev}" data-reflex="click->TabularReflex#paginate" href="#" ←
- pagy.series.each do |item|
- if item == :gap
li.page-item.disabled
a.page-link ...
- else
li class=("page-item #{"active" if item.is_a?(String)}")
a.page-link data-page=item data-reflex="click->TabularReflex#paginate" href="#" = item
li.page-item
a.page-link data-page="#{pagy.next}" data-reflex="click->TabularReflex#paginate" href="#" →
// javascript/stylesheets/applications.scss
body.wait, body.wait * {
cursor: wait !important;
}
// javascript/controllers/application_controller.js
import Turbolinks from 'turbolinks'
import { Controller } from 'stimulus'
import StimulusReflex from 'stimulus_reflex'
export default class extends Controller {
connect () {
StimulusReflex.register(this)
}
beforeReflex (element, reflex) {
//adding cursor spinner
document.body.classList.add('wait')
}
afterReflex (element, reflex) {
//some jquery plugins reinitialization
$('[data-toggle="tooltip"]').tooltip()
$('[data-toggle="popover"]').popover()
$("[type='checkbox']").bootstrapSwitch()
document.querySelectorAll('[data-uppy]').forEach(element => setupUppy(element))
OverlayScrollbars(document.querySelectorAll(".sidebar"), {})
//removing spinner
document.body.classList.remove('wait')
}
}
# controllers/categories_controller.rb
class CategoriesController < ApplicationController
before_action :authenticate_user!
def index
@page = (session[:page] || 1).to_i
categories = Category.includes(:products)
pages = (categories.count / Pagy::VARS[:items].to_f).ceil
@page = 1 if @page > pages
@pagy, @categories = pagy(categories, page: @page)
end
end
/views/categories/index.html.slim
/...categories table body & paginator partial
tbody
= render @categories
== render 'shared/paginator_nav', pagy: @pagy if @pagy.pages > 1
# reflexes/tabular_reflex.rb
class TabularReflex < ApplicationReflex
before_reflex :switch_tenant
def paginate
session[:page] = element.dataset[:page].to_i
end
private
def switch_tenant
Apartment::Tenant.switch! tenant
end
end
@RolandStuder
Copy link

Could you post an entire repo, so we can reproduce the problem. I don't see an error in the code. What does sometimes mean? You need to be more specific when it happens, it is very unlikely it just works sometims and sometimes it wont unless something changed in between.

While there is no error, you should really get away, from this global style of doing everything in the application_controller. You should have small focused stimulus.js controllers (as I wrote here: stimulusreflex/stimulus_reflex#246 (comment)). What you do is not really the stimulus way, you are just forcing jQuery into the stimulus world.

The way you implemented it, you the wait should trigger on every reflex. You really don't want that going into the future, especially with the new possibilities to morph specific things, you should aim to make reflexes so fast, that no waiting indicator is needed. If you have fast reflexed this wait class will just lead to unwanted irritating flashed.

But anyway can't help unless you put a full repo.

@matedemorphy
Copy link
Author

@RolandStuder thanks for your answer. Putting jQuery aside, "sometimes" it means that: this is the paginator-> |1| |2| |3| |4| |5| |6| |7| |8| |9| |10|

If i click in the |2| it won't executes afterReflex() wich i need to remove the spinner, but if i click in any other page, then it will. When i click in a page next to the current afterReflex() won't fire, only in those cases it miss the callback. Thats what i mean with "sometimes".

this is the repo: https://github.com/matedemorphy/pixcart-saas

you will need to install and run mailcatcher.

@matedemorphy
Copy link
Author

@RolandStuder solved by adding data-id

@RolandStuder
Copy link

@matedemorphy Glad you worked it out! Good luck with your further Stimulus Reflex endeavours!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment