inesita-rb / inesita

Frontend web application framework in Ruby using Opal.
https://inesita.fazibear.me/
MIT License
259 stars 15 forks source link

Global namespace for all stores. Is this for good? #13

Closed olegantonyan closed 8 years ago

olegantonyan commented 8 years ago

Hi! I'm wondering why all stores must be included in one class / global namespace. In general, this is not good idea, but maybe I still didn't get the idea of flux architecture?

Wouldn't be something like this a better approach?

class Store
  include Inesita::Store

  def method_missing(m, *args, &block) # just for illustration of the idea
    "#{m}_store".camelize.constantize.instance do
      render!
    end
  end

  def respond_to_missing?(m, include_private = false)
    true
  end
end

# all stores are singletones
class ApplicationStore
  class << self
    def instance(&block)
      @render_block = block
      @instance ||= new
    end

    private :new

    def render_block
      @render_block
    end
  end

  def render!
    self.class.render_block.call
  end
end

class MediaItemsStore < ApplicationStore
  def save_some_stuff
     render!
  end
end

# use it inside components
store.media_items.save_some_stuff

Now each store has its own namespace in store object, no need to include and initialize each store plus they are lazy-initialized

fazibear commented 8 years ago

Store class is just a wrapper. You can implement it whatever you like.