Example usage for com.google.gwt.query.client.plugins.deferred PromiseFunction PromiseFunction

List of usage examples for com.google.gwt.query.client.plugins.deferred PromiseFunction PromiseFunction

Introduction

In this page you can find the example usage for com.google.gwt.query.client.plugins.deferred PromiseFunction PromiseFunction.

Prototype

public PromiseFunction() 

Source Link

Usage

From source file:org.lirazs.gbackbone.client.core.view.TemplateFactory.java

License:Apache License

public static Promise loadTemplate(final String filePath, final Options templateSettings) {
    Promise promise;// w  ww  .j  av a2s  .c  om
    String urlRoot = TEMPLATE_SETTINGS.get("urlRoot");

    if (templateSettings != null) {
        if (templateSettings.containsKey("urlRoot"))
            urlRoot = templateSettings.get("urlRoot", String.class);
    }
    final String finalFilePath = urlRoot + filePath;

    if (CACHED_TEMPLATES.containsKey(finalFilePath)) {
        promise = new PromiseFunction() {
            @Override
            public void f(Deferred dfd) {
                dfd.notify(1);
                dfd.resolve(CACHED_TEMPLATES.get(finalFilePath));
            }
        };
    } else {
        promise = new PromiseFunction() {
            @Override
            public void f(final Deferred dfd) {

                final Ajax.Settings settings = Ajax.createSettings();
                settings.setUrl(finalFilePath);
                settings.setType("get");
                settings.setDataType("text");
                settings.setSuccess(new Function() {
                    @Override
                    public void f() {
                        String templateString = getArgument(0);
                        Template template = template(templateString, templateSettings);

                        CACHED_TEMPLATES.put(finalFilePath, template);

                        dfd.notify(1);
                        dfd.resolve(template);
                    }
                });
                GQuery.ajax(settings);
            }
        };
    }

    return promise;
}