Sockpuppet Expo


Increment

Increment 0
python
core/views/example.py (LOC 16)
from django.views.generic.base import TemplateView
from .mixins import ExampleMixin

class ExampleView(ExampleMixin, TemplateView):
    demo_template = '_example_demo.html'
    subtitle = 'Increment'

    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        context['count'] = self.request.session.get("count", 0)
        return context

example = ExampleView.as_view()

core/reflexes/example_reflex.py (LOC 8)
from sockpuppet.reflex import Reflex


class ExampleReflex(Reflex):
    def increment(self, step=1):
        self.count = int(self.element.dataset['count']) + step
        self.session['count']= self.count
javascript
core/javascript/controllers/example_controller.js (LOC 16)
import { Controller } from 'stimulus';
import StimulusReflex from 'stimulus_reflex';


export default class extends Controller {
  connect() {
    StimulusReflex.register(this)
  }

  increment(event) {
    console.log('increment')
    event.preventDefault()
    this.stimulate('ExampleReflex#increment', 1)
  }
}
html
core/templates/_example_demo.html (LOC 11)
<section>
  <a
    href="#"
    data-controller="example"
    data-action="click->example#increment"
    data-count="{{ count }}"
  >
    Increment {{ count }}
  </a>
</section>