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

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

Introduction

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

Prototype

Callbacks resolve

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

Click Source Link

Usage

From source file:org.lirazs.gbackbone.client.core.net.LocalStorageSyncStrategy.java

License:Apache License

@Override
public Promise sync(String method, Synchronized syncModel, Options options) {
    JSONValue response = null;/*from  w  ww  . j a  v  a  2s .com*/
    String errorMessage = null;

    Deferred syncDfd = new Deferred();

    try {
        if (method.equals("read")) {
            if (syncModel instanceof Model) {
                Model model = (Model) syncModel;
                response = find(model.getId()).toJsonValue();

            } else if (syncModel instanceof Collection) {
                response = findAll().toJsonValue();
            }
        } else if (method.equals("create")) {
            response = create((Options) syncModel.toJSON()).toJsonValue();
        } else if (method.equals("update")) {
            response = update((Options) syncModel.toJSON()).toJsonValue();
        } else if (method.equals("delete")) {
            response = destroy((Options) syncModel.toJSON()).toJsonValue();
        }
    } catch (Exception e) {
        errorMessage = e.getMessage();
    }

    if (response != null) {
        syncModel.trigger("sync", syncModel, response, options);

        if (options != null && options.containsKey("success")) {
            Function success = options.get("success");
            success.f(response);
        }
        syncDfd.resolve(response);
    } else {
        errorMessage = errorMessage != null ? errorMessage : "Record Not Found";

        if (options != null && options.containsKey("error")) {
            Function error = options.get("error");
            error.f(errorMessage);
        }
        syncDfd.reject(errorMessage);
    }

    // add compatibility with $.ajax
    // always execute callback for success and error
    if (options != null && options.containsKey("complete")) {
        Function complete = options.get("complete");
        complete.f(response);
    }

    return syncDfd.promise();
}

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;//from  ww w.  j ava2s  .  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;
}