com.cgxlib.xq.client.plugins.ajax.AjaxTransportJs.java Source code

Java tutorial

Introduction

Here is the source code for com.cgxlib.xq.client.plugins.ajax.AjaxTransportJs.java

Source

/*
 * Copyright 2014, The gwtquery team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cgxlib.xq.client.plugins.ajax;

/*
 * #%L
 * CGXlib
 * %%
 * Copyright (C) 2016 CGXlib (http://www.cgxlib.com)
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
  Code is originally from gwtquery, and modified by CGXlib team.
 */

import com.cgxlib.xq.client.Function;
import com.cgxlib.xq.client.Promise;
import com.cgxlib.xq.client.XQ;
import com.cgxlib.xq.client.plugins.ajax.Ajax.AjaxTransport;
import com.cgxlib.xq.client.plugins.ajax.Ajax.Settings;
import com.cgxlib.xq.client.plugins.deferred.PromiseFunction;
import com.cgxlib.xq.client.plugins.deferred.PromiseReqBuilder;
import com.cgxlib.xq.client.plugins.deferred.PromiseReqBuilderJSONP;
import com.google.gwt.core.client.Callback;
import com.google.gwt.core.client.ScriptInjector;
import com.google.gwt.dom.client.ScriptElement;

/**
 * Ajax transport for Client side.
 */
public class AjaxTransportJs implements AjaxTransport {

    @Override
    public Promise getJsonP(Settings settings) {
        return new PromiseReqBuilderJSONP(settings.getUrl(), settings.getTimeout());
    }

    @Override
    public Promise getLoadScript(final Settings settings) {
        return new PromiseFunction() {
            private ScriptElement scriptElement;

            public void f(final Deferred dfd) {
                scriptElement = ScriptInjector.fromUrl(settings.getUrl()).setWindow(XQ.window)
                        .setCallback(new Callback<Void, Exception>() {
                            public void onSuccess(Void result) {
                                XQ.$(XQ.window).delay(0, new Function() {
                                    public void f() {
                                        dfd.resolve(scriptElement);
                                    }
                                });
                            }

                            public void onFailure(Exception reason) {
                                dfd.reject(reason);
                            }
                        }).inject().cast();
            }
        };
    }

    public Promise getXhr(Settings settings) {
        return new PromiseReqBuilder(settings);
    }
}