Netzke::Javascript::ClassMethods

Public Instance Methods

include_js() click to toggle source

DEPRECATED. Returns an array of included files. Made to be overridden. js_include is preferred way.

# File lib/netzke/javascript.rb, line 227
def include_js
  []
end
js_alias() click to toggle source

Builds this component’s alias E.g.: netzke.basepack.window, netzke.basepack.gridpanel

MAV from stackoverflow.com/questions/5380770/replacing-ext-reg-xtype-in-extjs4 “When you use an xtype in Ext JS 4 it looks for a class with an alias of ‘widget.[xtype]’”

# File lib/netzke/javascript.rb, line 190
def js_alias
  [js_alias_prefix, js_xtype].join(".")
end
js_alias_prefix() click to toggle source

Alias prefix. Overridden for plugins.

# File lib/netzke/javascript.rb, line 181
def js_alias_prefix
  "widget"
end
js_base_class(class_name = nil) click to toggle source

Used it to specify what JavaScript class this component’s JavaScript class will be extending, e.g.:

js_base_class "Ext.TabPanel"

By default, “Ext.Panel” is assumed.

If called without parameters, returns the JS base class declared for the component.

# File lib/netzke/javascript.rb, line 34
def js_base_class(class_name = nil)
  class_name.nil? ? (read_inheritable_attribute(:js_base_class) || "Ext.Panel") : write_inheritable_attribute(:js_base_class, class_name)
end
js_class() click to toggle source

Component’s JavaScript class declaration. It gets stored in the JS class cache storage (Netzke.classes) at the client side to be reused at the moment of component instantiation.

# File lib/netzke/javascript.rb, line 196
      def js_class
        res = []
        # Defining the scope if it isn't known yet
        res << %{Ext.ns("#{js_full_scope}");} unless js_full_scope == js_default_scope
        res << (extends_netzke_component? ? js_class_declaration_extending_component : js_class_declaration_new_component)
        # Store created class xtype in the cache
        res << %(
Netzke.cache.push('#{js_xtype}');
)
        res.join("\n")
      end
js_code() click to toggle source

JavaScript code needed for this particulaer class. Includes external JS code and the JS class definition for self.

# File lib/netzke/javascript.rb, line 232
def js_code
  [js_included, js_class].join("\n")
end
js_include(*args) click to toggle source

Use it to specify JS files to be loaded before this component’s JS code. Useful when using external extensions required by this component. It may accept one or more symbols or strings. Strings will be interpreted as full paths to included JS file:

js_include "#{File.dirname(__FILE__)}/my_component/one.js","#{File.dirname(__FILE__)}/my_component/two.js"

Symbols will be expanded following a convention, e.g.:

class MyComponent < Netzke::Base
  js_include :some_library
  # ...
end

This will “include” a JavaScript file +{component_location}/my_component/javascripts/some_library.js+

# File lib/netzke/javascript.rb, line 72
def js_include(*args)
  callr = caller.first
  self.js_included_files += args.map{ |a| a.is_a?(Symbol) ? expand_js_include_path(a, callr) : a }
end
js_included() click to toggle source

Returns all included JavaScript files as a string

# File lib/netzke/javascript.rb, line 213
def js_included
  res = ""
  # Prevent re-including code that was already included by the parent
  # (thus, only include those JS files when include_js was defined in the current class, not in its ancestors)
  ((singleton_methods(false).map(&:to_sym).include?(:include_js) ? include_js : []) + js_included_files).each do |path|
    f = File.new(path)
    res << f.read << "\n"
  end
  res
end
js_method(name, definition = nil) click to toggle source

Use it to define a public method of the component’s JavaScript class, e.g.:

js_method :do_something, <<-JS
  function(params){
    // implementation, maybe dynamically generated
  }
JS

This will effectively result in definition of a public method called doSomething in the JavaScript class (note the conversion from underscore_name to camelCaseName).

# File lib/netzke/javascript.rb, line 47
def js_method(name, definition = nil)
  definition = yield.l if block_given?
  current_js_methods = read_clean_inheritable_hash(:js_methods)
  current_js_methods.merge!(name => definition.l)
  write_inheritable_attribute(:js_methods, current_js_methods)
end
js_methods() click to toggle source

Returns all JS method definitions in a hash

# File lib/netzke/javascript.rb, line 55
def js_methods
  read_clean_inheritable_hash(:js_methods)
end
js_mixin(*args) click to toggle source

Use it to “mixin” JavaScript objects defined in a separate file.

You do not have to use js_method or js_properties if those methods or properties are not supposed to be changed dynamically (by means of configuring the component on the class level). Instead, you may “mixin” a JavaScript object defined in the JavaScript file named following a certain convention. This way static JavaScript code will rest in a corresponding .js file, not in the Ruby class. E.g.:

class MyComponent < Netzke::Base
  js_mixin :some_functionality
  #...
end

This will “mixin” a JavaScript object defined in a file called +{component_location}/my_component/javascripts/some_functionality.js+, which way contain something like this:

{
  someProperty: 100,
  someMethod: function(params){
    // ...
  }
}

Also accepts a string, which will be interpreted as a full path to the file (useful for sharing mixins between classes). With no parameters, will assume :component_class_name_underscored.

# File lib/netzke/javascript.rb, line 161
def js_mixin(*args)
  args << name.split("::").last.underscore.to_sym if args.empty? # if no args provided, component_class_underscored_name is assumed
  current_mixins = read_clean_inheritable_array(:js_mixins) || []
  callr = caller.first
  args.each{ |a| current_mixins << (a.is_a?(Symbol) ? File.read(expand_js_include_path(a, callr)) : File.read(a))}
  write_inheritable_attribute(:js_mixins, current_mixins)
end
js_mixins() click to toggle source

Returns all objects to be mixed in (as array of strings)

# File lib/netzke/javascript.rb, line 170
def js_mixins
  read_clean_inheritable_array(:js_mixins) || []
end
js_properties(hsh = nil) click to toggle source

Used to define default properties of the JavaScript class, e.g.:

js_properties :collapsible => true, :hide_collapse_tool => true

(this will result in the definition of the following properties in the JavaScript class’s prototype: collapsible and hideCollapseTool (note the automatic conversion from underscore to camelcase))

Also, js_property can be used to define properties one by one.

For the complete list of available options refer to the Ext documentation, the “Config Options” section of a the component specified with js_base_class. Note, that not all the configuration options can be defined on the prototype of the class. For example, defining items on the prototype won’t take any effect, so, items should be passed as a configuration option at the moment of instantiation (see Netzke::Base#configuration and Netzke::Base#default_config).

# File lib/netzke/javascript.rb, line 88
def js_properties(hsh = nil)
  if hsh.nil?
    read_clean_inheritable_hash(:js_properties)
  else
    current_js_properties = read_clean_inheritable_hash(:js_properties)
    current_js_properties.merge!(hsh)
    write_inheritable_attribute(:js_properties, current_js_properties)
  end
end
js_property(name, value = nil) click to toggle source

Used to define a single JS class property, e.g.:

js_property :title, "My Netzke Component"
# File lib/netzke/javascript.rb, line 100
def js_property(name, value = nil)
  name = name.to_sym
  if value.nil?
    (read_inheritable_attribute(:js_properties) || {})[name]
  else
    current_js_properties = read_clean_inheritable_hash(:js_properties)
    current_js_properties[name] = value
    write_inheritable_attribute(:js_properties, current_js_properties)
  end
end
js_translate(*properties) click to toggle source

Defines the “i18n” config property, that is a translation object for this component, such as:

i18n: {
  overwriteConfirm: "Are you sure you want to overwrite preset '{0}'?",
  overwriteConfirmTitle: "Overwriting preset",
  deleteConfirm: "Are you sure you want to delete preset '{0}'?",
  deleteConfirmTitle: "Deleting preset"
}

E.g.:

js_translate :overwrite_confirm, :overwrite_confirm_title, :delete_confirm, :delete_confirm_title

TODO: make the name of the root property configurable

# File lib/netzke/javascript.rb, line 123
def js_translate(*properties)
  if properties.empty?
    read_clean_inheritable_hash(:js_translated_properties)
  else
    current_translated_properties = read_clean_inheritable_hash(:js_translated_properties)
    properties.each do |p|
      if p.is_a?(Hash)
        # TODO: make it possible to nest translated objects
      else
        current_translated_properties[p] = p
      end
    end
    write_inheritable_attribute(:js_translated_properties, current_translated_properties)
  end
end
js_xtype() click to toggle source

Builds this component’s xtype E.g.: netzkebasepackwindow, netzkebasepackgridpanel

# File lib/netzke/javascript.rb, line 176
def js_xtype
  name.gsub("::", "").downcase
end

Protected Instance Methods

js_class_declaration_extending_component() click to toggle source

Generates declaration of the JS class as extension of another Netzke component

# File lib/netzke/javascript.rb, line 267
        def js_class_declaration_extending_component
          base_class = superclass.js_full_class_name
          mixins = js_mixins.empty? ? "" : %(#{js_mixins.join(", \n")}, )
          # Resulting JS:
%(Ext.define('#{js_full_class_name}', Netzke.chainApply(#{mixins}#{js_extend_properties.to_nifty_json}, {
  extend: '#{base_class}',
  alias: '#{js_alias}'
}));)
        end
js_class_declaration_new_component() click to toggle source

Generates declaration of the JS class as direct extension of a Ext component

# File lib/netzke/javascript.rb, line 252
        def js_class_declaration_new_component
          mixins = js_mixins.empty? ? "" : %(#{js_mixins.join(", \n")}, )
          # Resulting JS:
%(Ext.define('#{js_full_class_name}', Netzke.chainApply({
  extend: '#{js_base_class}',
  alias: '#{js_alias}',
  constructor: function(config) {
    Netzke.aliasMethodChain(this, "initComponent", "netzke");
    #{js_full_class_name}.superclass.constructor.call(this, config);
  }
}, Netzke.componentMixin,\n#{mixins} #{js_extend_properties.to_nifty_json}));)
        end
js_extend_properties() click to toggle source

JS properties and methods merged together

# File lib/netzke/javascript.rb, line 247
def js_extend_properties
  @js_extend_properties ||= js_properties.merge(js_methods)
end
null() click to toggle source

Little helper. E.g.:

js_property :load_mask, null
# File lib/netzke/javascript.rb, line 244
def null; "null".l; end
this() click to toggle source

Little helper

# File lib/netzke/javascript.rb, line 239
def this; "this".l; end

[Validate]

Generated with the Darkfish Rdoc Generator 2.