Package com.google.gwt.query.client

Examples of com.google.gwt.query.client.Function


 
  public void testJsonValidService() {
    delayTestFinish(5000);
    // Use a public json service supporting callback parameter
    Ajax.getJSONP("https://www.googleapis.com/blogger/v2/blogs/user_id/posts/post_id?callback=?&key=NO-KEY")
      .done(new Function(){
        public void f() {
          IsProperties p = arguments(0);
          // It should return error since we do not use a valid key
          // {"error":{"errors":[{"domain":"usageLimits","reason":"keyInvalid","message":"Bad Request"}],"code":400,"message":"Bad Request"}}
          assertEquals(400, p.<IsProperties>get("error").<Number>get("code").intValue());
View Full Code Here


  public void testAjaxError() {
    delayTestFinish(5000);
    String url = "http://127.0.0.1/nopage";

    Ajax.ajax(Ajax.createSettings().setTimeout(1000).setUrl(url))
      .done(new Function(){
        public void f() {
          fail();
        }
      }).fail(new Function(){
        public void f() {
          finishTest();
        }
      });
  }
View Full Code Here

 
  public void testLoadScript() {
    delayTestFinish(5000);
    String url = "http://code.jquery.com/jquery-2.0.3.min.js";
    Ajax.loadScript(url)
      .done(new Function(){
        public void f() {
          finishTest();
        }
      }).fail(new Function(){
        public void f() {
          fail();
        }
      });
  }
View Full Code Here

  public void testGetScriptFail() {
    delayTestFinish(5000);
    String url = "http://127.0.0.1/nopage";
    Ajax.getScript(url)
      .done(new Function(){
        public void f() {
          fail();
        }
      }).fail(new Function(){
        public void f() {
          finishTest();
        }
      });
 
View Full Code Here

   * @param settings a Properties object with the configuration of the Ajax request.
   */
  public static Promise ajax(Settings settings) {
    resolveSettings(settings);
   
    final Function onSuccess = settings.getSuccess();
    if (onSuccess != null) {
      onSuccess.setElement(settings.getContext());
    }

    final Function onError = settings.getError();
    if (onError != null) {
      onError.setElement(settings.getContext());
    }

    final String dataType = settings.getDataType();

    Promise ret = null;

    if ("jsonp".equalsIgnoreCase(dataType)) {
      ret = GQ.getAjaxTransport().getJsonP(settings);
    } else if ("loadscript".equalsIgnoreCase(dataType)){
      ret = GQ.getAjaxTransport().getLoadScript(settings);
    } else {
      ret = GQ.getAjaxTransport().getXhr(settings)
        .then(new Function() {
          public Object f(Object...args) {
            Response response = arguments(0);
            Request request = arguments(1);
            Object retData = response.getText();
            if (retData != null && !"".equals(retData)) {
              try {
                if ("xml".equalsIgnoreCase(dataType)) {
                  retData = JsUtils.parseXML(response.getText());
                } else if ("json".equalsIgnoreCase(dataType)) {
                  retData = GQ.create(response.getText());
                } else {
                  retData = response.getText();
                  if ("script".equalsIgnoreCase(dataType)) {
                    ScriptInjector.fromString((String)retData).setWindow(window).inject();
                  }
                }
              } catch (Exception e) {
                if (GWT.isClient() && GWT.getUncaughtExceptionHandler() != null) {
                  GWT.getUncaughtExceptionHandler().onUncaughtException(e);
                } else {
                  e.printStackTrace();
                }
              }
            }
            return new Object[]{retData, "success", request, response};
          }
        }, new Function() {
          public Object f(Object...args) {
            Throwable exception = arguments(0);
            Request request = getArgument(1, Request.class);
            String msg = String.valueOf(exception);
            return new Object[]{null, msg, request, null, exception};
View Full Code Here

    final String filter = url.contains(" ") ? url.replaceFirst("^[^\\s]+\\s+", "") : "";
    s.setUrl(url.replaceAll("\\s+.+$", ""));
    s.setDataType("html");
    s.setType("get");
    s.setData(data);
    s.setSuccess(new Function() {
      public void f() {
        try {
          // We clean up the returned string to smoothly append it to our document
          // Note: using '\s\S' instead of '.' because gwt String emulation does
          // not support java embedded flag expressions (?s) and javascript does
View Full Code Here

   */
  public Promise promise(final String name) {
    final Promise.Deferred dfd = Deferred();

    // This is the unique instance of the resolve function which will be added to each element.
    final Function resolve = new Function() {
      // Because it is an inner function, the counter cannot final outside the function
      int count = 1;
      // Inner functions don't have constructors, we use a block to initialize it
      {
        for (Element elem: elements()) {
          // Add this resolve function only to those elements with active queue
          if (queue(elem, name, null) != null) {
            emptyHooks(elem, name).add(this);
            count++;
          }
        }
      }
     
      public void f() {
        if (--count == 0) {
          dfd.resolve(QueuePlugin.this);
        }
      }
    };
   
    // Run the function and resolve it in case there are not elements with active queue
    resolve.f(this, name);
   
    return dfd.promise();
  }
View Full Code Here

    }
  }
 
  private void runNext(Element elem, String name, Queue<? extends Function> q) {
    assert q != null;
    Function f = q.peek();
    if (f != null) {
      f.fe(elem);
    } else {
      // Run final hooks when emptying the queue, used in promises
      emptyHooks(elem, name).fire();
      // It is the last function, remove the queue to avoid leaks (issue 132)
      removeData(elem, name);
View Full Code Here

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

    return null;
  }

  private String result = "";
  public void testCallbacks() {
    final Function fn1 = new Function() {
      public Object f(Object...arguments) {
        String s = " f1:";
        for (Object o: arguments){
          s += " " + o;
        }
        result += s;
        return false;
      }
    };
   
    final Callback fn2 = new Callback() {
      public boolean f(Object... objects) {
        String s = " f2:";
        for (Object o: objects){
          s += " " + o;
        }
        result += s;
        return false;
      }
    };
   
    com.google.gwt.core.client.Callback<Object, Object> fn3 = new com.google.gwt.core.client.Callback<Object, Object>() {
      public void onFailure(Object reason) {
        result += " f3_fail: " + reason;
      }
      public void onSuccess(Object objects) {
        String s = " f3_success:";
        for (Object o: (Object[])objects){
          s += " " + o;
        }
        result += s;
      }
    };
   
    result = "";
    Callbacks callbacks = new Callbacks();
    callbacks.add( fn1 );
    callbacks.fire( "foo" );
    assertEquals(" f1: foo", result);
   
    result = "";
    callbacks.add( fn2 );
    callbacks.fire( "bar" );
    assertEquals(" f1: bar f2: bar", result);

    result = "";
    callbacks.remove( fn2 );
    callbacks.fire( "foobar" );
    assertEquals(" f1: foobar", result);

    result = "";
    callbacks.add( fn1 );
    callbacks.fire( "foo" );
    assertEquals(" f1: foo f1: foo", result);

    result = "";
    callbacks = new Callbacks("unique");
    callbacks.add( fn1 );
    callbacks.add( fn1 );
    callbacks.fire( "foo" );
    assertEquals(" f1: foo", result);

    result = "";
    callbacks.add( fn3 );
    callbacks.fire( "bar" );
    assertEquals(" f1: bar f3_success: bar", result);
   
    result = "";
    callbacks = new Callbacks("memory");
    callbacks.add( fn1 );
    callbacks.fire( "foo" );
    callbacks.add( fn2 );
    callbacks.fire( "bar" );
    callbacks.remove(fn2);
    callbacks.fire( "foobar" );
    assertEquals(" f1: foo f2: foo f1: bar f2: bar f1: foobar", result);

    result = "";
    callbacks = new Callbacks("stopOnFalse");
    callbacks.add( fn1 );
    callbacks.add( fn2 );
    callbacks.fire( "bar" );
    assertEquals(" f1: bar", result);
   
    result = "";
    callbacks.disable();
    callbacks.fire( "bar" );
    assertEquals("", result);

    result = "";
    callbacks = new Callbacks("memory once unique");
    callbacks.add( fn1 );
    callbacks.add( fn1 );
    callbacks.fire( "bar" );
    assertEquals(" f1: bar", result);
    callbacks.fire( "foo" );
    assertEquals(" f1: bar", result);
    callbacks.add( fn2 );
    callbacks.add( fn2 );
    assertEquals(" f1: bar f2: bar f2: bar", result);
    callbacks.remove( fn1 );
    callbacks.add( fn1 );
    assertEquals(" f1: bar f2: bar f2: bar f1: bar", result);
    callbacks.remove( fn1 );
    callbacks.disable();
    callbacks.add( fn1 );
    assertEquals(" f1: bar f2: bar f2: bar f1: bar f1: bar", result);
   
    // Test adding callback functions in nested executions
    result = "";
    final Callbacks callbacks2 = new Callbacks("memory once unique");
    callbacks2.add(fn1);
    callbacks2.add(new Function(){public void f() {
      callbacks2.add( fn2 );
    }});
    callbacks2.fire("foo");
    assertEquals(" f1: foo f2: foo", result);
  }
View Full Code Here

TOP

Related Classes of com.google.gwt.query.client.Function

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.