MarcusCalidus / marcuscalidus-svg-panel

Grafana panel for displaying metric sensitive SVG images using the snap svg library
MIT License
62 stars 28 forks source link

Feature request: context #62

Open wz2b opened 3 years ago

wz2b commented 3 years ago

I have a feature request that I think would be non-breaking.

I would like .onInit() to be able to return an item - let's call it a state or context - that is then passed as a 2rd parameter to onHandleMetric() ... for example:

  // function onInit(ctrl, svgnode) {
         var s = Snap(svgnode)
         var content = s.select("#content")
         var r = content.rect(10, 10, 100, 100, 15, 15)

       // return a new state or context object that will be passed to onHandleMetric()
        return {
             theRect: r,
             counter: 0
        }
  // }
  // function onHandleMetric(ctrl, svgnode, state) {
        console.log("The counter is now", context.counter)
        context.counter = context.counter + 1
  // }

Some things can be done by re-selecting from the DOM but that's more expensive than just passing a reference to some objects you created, and it doesn't let you pass other things.

Does this seem reasonable?

wz2b commented 3 years ago

As a workaround, I added something to ctrl like this:

  // function onInit(ctrl, svgnode) {
        ctrl.privateState = {
          counter: 0
        }
  // }
  // function onHandleMetric(ctrl, svgnode) {
        console.log("The counter is now", ctrl.privateState.counter++)
  // }

This works, but I feel like it's unsafe.