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

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

Introduction

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

Prototype

Promise promise

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

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  www  . j  a  v a2 s . c om*/
    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();
}