Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for 
change notification.

The following page has been changed by DavorHrg:
http://wiki.apache.org/tapestry/Tapestry5HowToAddBindingPrefix

New page:
In this example we will create our own binding prefix to support array/list 
values directly in template without needing to write a getter method.[[BR]]
This is tested on (5.0.5), it will probably be a part of the framework in some 
way, but to show how easy is to extend tapestry....[[BR]]

For example if you want to link your product page, you will write this in 
tempalte :
{{{
<T:ActionLink page="Product" context="producViewLink">View</T:ActionLink>
<T:ActionLink page="Product" context="producEditLink">Edit</T:ActionLink>
}}}
and this in your java class
{{{
public Object[] getProductLink(){
    return new Object[]{product.getId(),"view"};
}
public Object[] getProductLink(){
    return new Object[]{product.getId(),"edit"};
}
}}}

After following this example, the java part will not be needed and template 
will look something like this:
{{{
<T:ActionLink page="Product" 
context="list:product.id,'view'">View</T:ActionLink>
<T:ActionLink page="Product" 
context="list:product.id,'edit'">Edit</T:ActionLink>
}}}



'''Now the example.'''

Add this to your !AppModule.java
{{{
    public static void contributeBindingSource(
            MappedConfiguration<String, BindingFactory> configuration,
            BindingSource bindingSource)
    {
        configuration.add("list",new ListBindingFactory(bindingSource));
    }
}}}

The !ListBindingFactory
{{{
/**
 * Implementation of the list: binding prefix -- we parse list of bindings
 * and generate delegate bindings for each element<br>
 * default binding is prop, except when surrounded with '' or is a numeric 
value ([0-9.]*)
 */
public class ListBindingFactory implements BindingFactory {
    private final BindingSource _bindingSource;

    public ListBindingFactory(BindingSource source){
        this._bindingSource = source;
    }
    
    public Binding newBinding(String description, ComponentResources container, 
ComponentResources component,
            String expression, Location location)
    {
        List<Binding> delegates = new ArrayList<Binding>();
        String[] bindingNames = expression.split(",");
        
        for (String bindingName : bindingNames){
            String defaultBinding = TapestryConstants.LITERAL_BINDING_PREFIX;
            
            if(bindingName.charAt(0) == '\''){
                //translate "'something'" to "literal:something"
                bindingName = bindingName.substring(1,bindingName.length()-1);

            }else{
                //if value is numeric, we leave literal binding prefix as 
default
                for (int i = 0; i < bindingName.length(); i++) {
                    char ch = bindingName.charAt(i);
                    if(ch != '.' && !Character.isDigit(ch)){
                        defaultBinding = TapestryConstants.PROP_BINDING_PREFIX; 
                        break;
                    }
                }
            }

            delegates.add(_bindingSource.newBinding(description, container, 
component, defaultBinding, bindingName, location));
        }
        
        return new ListBinding(delegates);
    }
}
}}}

The !ListBinding 
{{{
public class ListBinding extends AbstractBinding{
    private final List<Binding> delegates;

    public ListBinding(List<Binding> delegates) {
        this.delegates = delegates;
    }

    public Object get() {
        Object[] valuesFromDelegates = new Object[delegates.size()];

        for (int i = 0; i < delegates.size(); i++) {
            valuesFromDelegates[i] = delegates.get(i).get();
        }
        
        return valuesFromDelegates;
    }
    
    @Override
    public Class<Object[]> getBindingType() {
        return Object[].class;
    }
}
}}}
!Notice: although we named this binding "list" the value returned is an Object 
array, but tapestry will easily convert between the two. If you like, you can 
easily change this to return "List"


And that's it, you now have your own binding prefix. It splits the expression 
on "," and each subexpression will be evaluated as it was single binding 
expression (with exception for numeric values and values surrounded with ' ', 
which are prefixed literal).

for example a chart link (similar to chart example on this wiki)
{{{
<t:chart width="200" height="150" context="list:'aa',22,'bb',5" 
popup="popupSize"/>
<t:chart width="200" height="150" context="list:'aa',29,'bb',30,'cc',10" 
popup="popupSize"/>
}}}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to