Skip to content

A framework for elegant Sencha Ext JS Ruby on Rails components

License

Notifications You must be signed in to change notification settings

Drunik01/netzke-core

 
 

Repository files navigation

Netzke Core Gem Version Build Status Code Climate

RDocs

Netzke Core is the bare bones of the Netzke framework. For pre-built full-featured components (like grids, forms, tab/accordion panels, etc), see netzke-basepack.

For rationale and mini-tutorial, refer to the meta gem's README. Current README explains the Netzke architecture in some detail. Some knowledge of Sencha Ext JS (and Ruby, for that matter) may be required to fully understand this overview.

What is a Netzke component

A Netzke component is a Ruby class (further referred to as "server class"), which is being represented by an Ext JS Component on the server-side (further referred to as "client class"). The responsibility of the server class is to "assemble" the client class and provide the configuration for its instance (further referred as "client class instance"). Even if it may sound a bit complicated, Netzke provides a simple API for defining and configuring the client class. See Client class for details.

Further, each Netzke component inherits convenient API for enabling the communication between the client and server class. See Client-server interaction for details.

With Netzke components being a Ruby class, and the client class being incapsulated in it, it is possible to use a Netzke component in your application by simply writing Ruby code. However, while creating a component, developers can fully use their Ext JS skills - Netzke puts no obstacles here.

A typical Netzke component's code is structured like this (on example of MyComponent):

your_web_app
  app
    components
      my_component.rb             <-- the Ruby class
      my_component
        some_module.rb            <-- optional extra Ruby code
        javascripts
          some_dependency.js      <-- optional external JS library
          init_component.js       <-- optional mixins to the client class
          extra_functionality.js  <-- more mixins (mixin-in may depend on component class configuration)
        stylesheets
          my_special_button.css    <-- optional custom CSS

Client class

The generated client class is inherited (as defined by the Ext JS class system) from an Ext JS class, which by default is Ext.panel.Panel. For example, a component defined like this:

class HelloWorld < Netzke::Base
end

will have the following client class generated by Netzke (simplified):

Ext.define('Netzke.classes.HelloWorld', {"extend":"Ext.panel.Panel", "mixins":["Netzke.classes.Core.Mixin"]});

Netzke.classes.Core.Mixin contains a set of client class methods and properties common to every Netzke component.

Extending HelloWorld will be automatically reflected on the client-class level:

class HelloNewWorld < HelloWorld
end

will have the following client class generated (simplified):

Ext.define('Netzke.classes.HelloNewWorld', {"extend":"Netzke.classes.HelloWorld"});

The configuration of a client-class is done by using the Netzke::Base.js_configure. For example, in order to inherit from a different Ext JS component, and to mix in the methods defined in an external JavaScript class:

class MyTabPanel < Netzke::Base
  js_configure do |c|
    c.extend = "Ext.tab.Panel"
    c.mixin :extra_functionality
  end
end

For more details on defining the client class, refer to Netzke::Core::ClientClass.

Composition

Any Netzke component can define child components, which can either be statically nested in the compound layout (e.g. as different regions of the 'border' layout), or dynamically loaded at a request (as in the case of the edit form window in Netzke::Basepack::GridPanel, for example).

Defining child components

You can define a child component by calling the component class method which normally requires a block:

component :users do |c|
  c.klass = GridPanel
  c.model = "User"
  c.title = "Users"
end

Nesting components

Declared components can be referred to in the component layout:

def configure(c)
  super
  c.items = [
    { xtype: :panel, title: "Simple Ext panel" },
    :users
  ]
end

Dynamic loading of components

Next to being statically nested in the layout, a child component can also be dynamically loaded by using client class' netzkeLoadComponent method:

this.netzkeLoadComponent('users');

this will load the "users" component and add it to the current container.

For more details on dynamic component loading refer to inline docs of javascript/ext.js.

For more details on composition refer to Netzke::Core::Composition.

Actions, toolbars, and menus

Actions are used by Ext JS to share functionality and state among multiple buttons and menu items. Define actions with the action class method:

action :show_report do |c|
  c.text = "Show report"
  c.icon = :report
end

The icon for this button will be images/icons/report.png (see Icons support).

Refer to actions in toolbars:

def configure(c)
  super
  c.bbar = [:show_report]
end

Actions can also be referred to is submenus:

  c.tbar = [{text: 'Menu', menu: {items: [:show_report]}}]

For more details on composition refer to Netzke::Core::Actions.

Client-server interaction

Communication between the client class and the corresponding server class is done by means of defining endpoints. By defining an endpoint on the server, the client class automatically gets a method that is used to call the server.

Calling an endpoint from client class

By defining an endpoint like this:

class SimpleComponent < Netzke::Base
  endpoint :whats_up_server do |greeting|
  # ...
  end
end

...the client class will obtain a method called whatsUpServer, that can be called like this:

this.whatsUpServer(greeting, callback, scope);

The last 2 params are optional:

  • callback - function to be called after the server successfully processes the endpoint call; the function will receive, as its only argument, the result of the endpoint block execution
  • scope - context in which the callback function will be called; defaults to the component's instance

As of version 1.0, the endpoint may receive an arbitrary number of arguments, for example:

this.serverDoSomething('value 1', true, callback, scope);
class SimpleComponent < Netzke::Base
  endpoint :server_do_something do |arg_1, arg_2|
    # arg_1 == 'value 1'
    # arg_2 == true
  end
end

Calling client class methods from endpoint

An endpoint can instruct the client instance of the component to execute a set of methods in response, passing those methods arbitrary parameters, by using the magical this variable. For example:

class SimpleComponent < Netzke::Base
  endpoint :whats_up_server do
    this.set_title("Response from server")
    this.my_method
  end
end

Here the client class will first call its setTitle method (defined in Ext.panel.Panel) with parameter passed from the endpoint. Then a custom method myMethod will be called with no parameters.

For more details on client-server communication see Netzke::Core::Services.

Icons support

Netzke can optionally make use of icons for making clickable elements like buttons and menu items more visual. The icons should be (by default) located in app/assets/images/icons.

An example of specifying an icon for an action:

action :logout do |c|
  c.icon = :door
end

The logout action will be configured with public/assets/icons/door.png as icon.

For more details on using icons refer to Netzke::Core::Actions.

I18n

Netzke Core will automatically include Ext JS localization files based on current I18n.locale.

Also, Netzke Core uses some conventions for localizing actions. Refer to Netzke::Core::Actions.

HAML support (experimental)

Netzke provides support for HAML templates in case you don't want to put HTML into Ruby files.

Requirements

  • Ruby >= 2.0.0
  • Rails ~> 4.2.0
  • Ext JS = 5.1.0

Installation

$ gem install netzke-core

For the latest ("edge") stuff, instruct the bundler to get the gem straight from github:

gem 'netzke-core', github: "netzke/netzke-core"

By default, Netzke assumes that your Ext JS library is located in public/extjs. It can be a symbolic link, e.g.:

$ ln -s PATH/TO/YOUR/EXTJS/FILES public/extjs

(Make sure that the location of the license.txt distributed with Ext JS is exactly public/extjs/license.txt)

Running tests

The bundled spec/rails_app application used for automated testing can be easily run as a stand-alone Rails app. It's a good source of concise, focused examples. After starting the application, access any of the test components (located in spec/rails_app/app/components) by using the following url:

http://localhost:3000/netzke/components/{name of the component's class}

For example http://localhost:3000/netzke/components/Endpoints

To run a specific Mocha JS spec (located in spec/mocha) for a component, append ?spec={name of spec}, for example:

[http://localhost:3000/netzke/components/Endpoints?spec=endpoints](http://localhost:3000/components/Endpoints?spec=endpoints)

To run all the tests (from the gem's root):

$ rake

This assumes that the Ext JS library is located/symlinked in spec/rails_app/public/extjs. If you want to use Sencha CDN instead, run:

$ EXTJS_SRC=cdn rake

Contributions and support

Help developing Netzke by submitting a pull request when you think others can benefit from it.

If you feel particularily generous, you can support the author by donating a couple bucks a week at GitTip.

Useful links


© 2008-2015 Max Gorin, released under the MIT license (see LICENSE).

Note that Ext JS is licensed differently, and you may need to purchase a commercial license in order to use it in your projects!

About

A framework for elegant Sencha Ext JS Ruby on Rails components

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 73.1%
  • JavaScript 17.0%
  • CoffeeScript 6.8%
  • HTML 2.5%
  • Other 0.6%