Sha256: 58514d362f18018820139ee47e05954b9934447f8d725d7f2dc42c9d4c09f123

Contents?: true

Size: 1.98 KB

Versions: 11

Compression:

Stored size: 1.98 KB

Contents

##
# @class ExtJS::Component
#
class ExtJS::Component
  attr_accessor :config
  def initialize(params)
    @config = params#params.extract_options!
    @controller = @config.delete(:controller) unless @config[:controller].nil?

    @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

  ##
  # Adds a config {} or ExtJS::Component instance to this component's items collection.
  # NOTE:  When :partial option is used a String will of course be returned.  Otherwise an ExtJS::Component
  # instance will be returned.
  # @return {String/ExtJS::Component}
  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?
        options[:controller] = @controller unless @controller.nil?
        cmp = ExtJS::Component.new(options)
        @partial_config = nil
        @config[:items] << cmp
        return cmp
      end
    elsif !config.empty? && config.first.kind_of?(ExtJS::Component)
      cmp = config.first
      cmp.apply(@partial_config) unless @partial_config.nil?
      @partial_config = nil
      @config[:items] << cmp.config
      return cmp
    end
  end

  def to_json
    config.to_json
  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|handler|scope)\":\s?\"([a-zA-Z\.\[\]\(\)]+)\"/, '"\1":\2')
    "Ext.ComponentMgr.create(#{json});"
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
extjs-mvc-0.1.20 lib/extjs/component.rb
extjs-mvc-0.1.22 lib/extjs/component.rb
extjs-mvc-0.1.23 lib/extjs/component.rb
extjs-mvc-0.1.24 lib/extjs/component.rb
extjs-mvc-0.1.25 lib/extjs/component.rb
extjs-mvc-0.1.33 lib/extjs/component.rb
extjs-mvc-0.1.32 lib/extjs/component.rb
extjs-mvc-0.1.31 lib/extjs/component.rb
extjs-mvc-0.1.30 lib/extjs/component.rb
mvc-0.1.30 lib/extjs/component.rb
mvc-0.1.29 lib/extjs/component.rb