Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This section describes key classes which are essential while using Slice. They constitute a base for a day-to-day development.

ModelProvider

The com.cognifide.slice.api.provider.ModelProvider interface is a basic interface of Slice. It allows you to fetch an injectable object(s) mapped from specified resource or path. It's very similar to Guice com.google.inject.Injector because it is used for fetching objects of different classes. However it's more Sling-related because it always takes resource (or path) as a parameter, so that an object of the specified class can be mapped. You should use ModelProvider whenever there is a need for injecting a model mapped from a given resource (path).

...

Code Block
import com.cognifide.app.util.Currency;

import com.cognifide.slice.api.provider.ModelProvider;
import com.cognifide.slice.mapper.annotation.JcrProperty;
import com.cognifide.slice.mapper.annotation.SliceResource;
import com.google.inject.Inject;

@SliceResource
public class OrderModel {

    private final static String CONFIGURATION_PATH = "/content/app/configuration/jcr:content/currency";

    private final ModelProvider modelProvider;

    @JcrProperty
    private int value;

    @Inject
    public OrderModel(ModelProvider modelProvider) {
        this.modelProvider = modelProvider;
    }

    public int getValue() {
        Currency currency = modelProvider.get(Currency.class, CONFIGURATION_PATH);
        return formatToCurrency(value, currency);
    }
    ...
}

ChildrenProvider

The com.cognifide.slice.api.provider.ChildrenProvider interface allows you to get a list of child resources (paths) from under specified path.

...

MethodDescription
List<String> getChildren(final String path);Returns paths to child resources of a resource from under specified path. If a resource under specified path doesn't exist, empty list is returned. The method is not recursive - it reads only direct children.
List<Resource> getChildResources(final String path);Returns child resources of a resource from under specified path. If a resource under specified path doesn't exist, empty list is returned. The method is not recursive - it reads only direct children

Link, LinkBuilder

Thanks to com.cognifide.slice.api.link.Link and com.cognifide.slice.api.link.LinkBuilder interfaces you can easily manipulate on Sling links, e.g. setting selectors, extension, query string, path, etc.

...

Info
titleBest practice

There is no need to create Link objects if you're not going to modify any aspects of link. If you only want to display a link you can build it simply using String concatenation.

ClassToKeyMapper

The com.cognifide.slice.api.provider.ClassToKeyMapper interface allows you to obtain a com.google.inject.Key associated with a class specified by a String value. Thanks to this you can inject objects of classes specified by Strings rather than classes. In general, this interface is not widely used.