On 24/07/15 01:15, Thiago Augusto Ventura Lima wrote:
> Assume I have a setting like "List<Foo> foos", where Foo is something like
>
> class Foo {
> public String propertyA;
> public String propertyB;
> }
>
> What I'm trying to do is to default "foos" to an "one element list", say
> it "[Foo('valueA', 'valueB')]", when this class is firstly loaded and
> there's no instance.
repeatableProperty has a "default" field, which specifies the initial
list of values for the repeatable collection, if there isn't yet an
instance.
So basically you just need to add a way of fetching that default list
from Foo.
For example, you could do something like this:
--- Inside BarPublisher.java / wherever Foo is defined ---
public static final class Foo extends AbstractDescribableImpl<Foo> {
@Exported public final String propA;
@Exported public final String propB;
@DataBoundConstructor
public Foo(String a, String b) {
this.propA = a;
this.propB = b;
}
public static Foo[] getSomeDefaults() {
return new Foo[] { new Foo("valueA", "valueB") };
}
// ...
}
---------------------------------
--- BarPublisher/config.jelly ---
<j:invokeStatic
var="myWonderfulDefaults"
className="com.example.BarPublisher$Foo"
method="getSomeDefaults" />
<f:entry title="${%List of foos}" field="foos">
<f:repeatableProperty
field="foos"
default="${myWonderfulDefaults}"
add="${%Add foo...}" />
</f:entry>
---------------------------------
Though remember that users can delete the default "Foo" that appears and
save the job anyway, or they can upload job config directly via the API etc.
So you should probably additionally check in your
builder/publisher/whatever constructor whether the collection is empty,
and if so, apply the defaults (if that's what you want).
Regards,
Chris