How do I only set a state variable in a Store if it has not already been set?

79 Views Asked by At

I am using Hyperstack Stores and in the before_mount of my component I want to do:

before_mount do
  BridgeStore.show_card_sample ||= true
end

And in the store:

class BridgeStore < HyperStore
  class << self
    state_accessor :show_card_sample
  end
end

But the conditional assignment ||= is being triggered each time a component of this type is rendered.

I know I can get around this by setting a state variable in the store state_accessor :is_set and only set the other variables if that has not been set, but I was wondering if there is a better way round this?

1

There are 1 best solutions below

2
On BEST ANSWER

You should move the logic around initialization into your store. Remember that in Ruby your class instance variables can be initialized as the class is defined:

class BridgeStore < HyperStore
  @show_card_sample = true
  class << self
    state_accessor :show_card_sample
  end
end