Example usage for com.google.gwt.query.client.plugins.deferred Deferred notify

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

Introduction

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

Prototype

Callbacks notify

To view the source code for com.google.gwt.query.client.plugins.deferred Deferred notify.

Click 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  a 2 s.  c  o  m*/
    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;
}