Created
July 1, 2020 23:57
-
-
Save matedemorphy/94c6ad13b6aebd89862b5b14120c98df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// javascript/stylesheets/applications.scss | |
body.wait, body.wait * { | |
cursor: wait !important; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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') | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/views/categories/index.html.slim | |
/...categories table body & paginator partial | |
tbody | |
= render @categories | |
== render 'shared/paginator_nav', pagy: @pagy if @pagy.pages > 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 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.
@RolandStuder solved by adding data-id
@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
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.