Sha256: d29cd3e93ff37fb208b0a66ad3c84f57abb95837870e82530851c4bb7145db0a

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

##
# @class ExtJS::Component
#
class ExtJS::Component
  attr_accessor :config
  def initialize(controller, params)
    @config = params.extract_options!
    @controller = controller

    @config[:items] = [] if config[:items].nil?

    if container = @config.delete(:container)
      container.add(self)
    end
    @partial_config = nil
  end

  def apply(params)
    @config.merge!(params)
  end

  def add(*config)
    options = config.extract_options!
    if !options.keys.empty?
      if url = options.delete(:partial)
        # rendering a partial, cache the config until partial calls #add method.  @see else.
        @partial_config = options
        return @controller.render(:partial => url, :locals => {:container => self})
      else
        options.merge!(@partial_config) unless @partial_config.nil?
        @config[:items] << options
      end
    elsif !config.empty? && config.first.kind_of?(ExtJS::Component)
      cmp = config.first
      cmp.apply(@partial_config) unless @partial_config.nil?
      @config[:items] << cmp.config
    end
    @partial_config = nil
  end

  def render
    # If there are any listeners attached in json, we have to get rid of double-quotes in order to expose
    # the javascript object.
    # eg:  "listeners":"SomeController.listeners.grid" -> {"listeners":SomeController.listeners.grid, ...}
    json = @config.to_json.gsub(/\"listeners\":\s?\"([a-zA-Z\.\[\]\(\)]+)\"/, '"listeners":\1')
    "Ext.ComponentMgr.create(#{json});"
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
extjs-mvc-0.1.17 lib/extjs/component.rb
extjs-mvc-0.1.18 lib/extjs/component.rb
extjs-mvc-0.1.19 lib/extjs/component.rb