The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

ExtJS::AutoForm::Moose::Types - Manage extjs form generation for Moose types

SYNOPSIS

Those are in fact the current type reflections implemented, except for enum, which is treated in a special way so far:

    reflect 'Any'  => extjs { {
        xtype => "displayfield",
        fieldLabel=> "Unsupported field type"
    } };

    reflect 'Str'  => extjs { {
        xtype => "textfield",
        value => \&ExtJS::AutoForm::Moose::Types::value_or_default
    } };

    reflect 'Num'  => extjs { {
        xtype => "numberfield",
        allowDecimals => JSON::Any::true,
        value => \&ExtJS::AutoForm::Moose::Types::value_or_default
    } };

    reflect 'Int'  => extjs { {
        xtype => "numberfield",
        allowDecimals => JSON::Any::false,
        value => \&ExtJS::AutoForm::Moose::Types::value_or_default
    } };

    reflect 'Bool' => extjs { {
        xtype => "checkbox",
        checked => \&ExtJS::AutoForm::Moose::Types::value_or_default_bool
    } };

DESCRIPTION

This module does two things: hold the registry of known type contraints reflections, and provide a bit of curry to ease adding reflections for new types based on their type name.

Syntax curry

reflect 'type_name' => extjs { code returning a hash template }

Create a new moose type constraint to extjs field association.

The template parameter must be specified as a perl function that returns a hash that will be, at some point, encoded as JSON and sent to a browser. You can use callbacks as values on this hash, which will be called and it's result used as the generated key value (See callbacks in "TEMAPLTE FORMAT" below).

Template format

The template format is the one used by ExtJS Component class creation. This means it does all it's job using xtypes and does not use any javascript functions.

Customizing tamplate values using callbacks

Any template hash value can be a callback instead of a plain value, which allows further customization of the generated extjs description.

Those callbacks receive two parameters: the object instance (undef when generation has been called statically), and the Moose::Meta::Attribute instance for that attribute.

Example:

    sub enum_values($$) {
        my ($obj,$attribute) = @_;
        return $attribute->type_constraint->values;
    }

See "REFLECTION HELPERS" below for a list of helper callbacks provided by default.

Javascript functions on the generated result?

Using javascript isn't straight-forward because the generated result is usually encoded as JSON, which by itself does not support javascript functions.

In those cases you need to use javascript functions, you've got a few options:

  • Don't

    If you read the ExtJS API docs carefully, you'll notice there's really not that much that cannot be controlled through config parameters.

  • Extend the base ExtJS components

    It's not difficult, and you can register those as a new xtype.

  • Encode the generated structure yourself

    This seems like a dirty hack to me, and you'll probably be meriting for a place in hell, but...

    Since JSON does not allow functions, you cannot use any of the available JSON modules supported by JSON::Any. You can get around this by writing your own structure to JSON encoder that knows how to handle that.

    You could also replace any functions on the whole structure with tokens and replace those tokens on the resulting JSON.

REFLECTION HELPERS

The following subroutines are provided as helpers for common checks and transformations used on the ExtJS templates.

value_or_default

Returns the current value of this attribute for the given object, or it's default value if reflection was done directly on the class

value_or_default_bool

Does the same as the previous helper, but it returns a JSON boolean value suitable for JSON::Any encoding

required_attribute_bool

Returns a JSON true value when this attribute is required, false otherwise

 =cut
sub required_attribute_bool($$) {
    my ($obj,$attribute) = @_;
    return $attribute->is_required ? JSON::Any::true : JSON::Any::false;
}
enum_values

Returns an array containing the enum-type attribute values. See Moose::Meta::TypeConstraint::Enum.

AUTHOR

Quim Rovira, quim at rovira.cat

BUGS

Please report any bugs or feature requests to bug-moosex-extjs-reflection at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Moosex-ExtJS-Reflection. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc ExtJS::AutoForm::Moose::Types

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2011 Quim Rovira.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

ExtJS trademarks are property of Sencha Labs http://www.sencha.com