BarrieH
BarrieH

Reputation: 373

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

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?

Upvotes: 1

Views: 75

Answers (1)

Mitch VanDuyn
Mitch VanDuyn

Reputation: 2878

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

Upvotes: 1

Related Questions