Fn.preload accepts null argument. Don't initialize knockout in static constructor, as the context may not yet be ready
1.1 --- a/boot/src/main/java/org/apidesign/html/boot/spi/Fn.java Sun Feb 09 23:26:38 2014 +0100
1.2 +++ b/boot/src/main/java/org/apidesign/html/boot/spi/Fn.java Tue Feb 11 21:18:03 2014 +0100
1.3 @@ -124,32 +124,40 @@
1.4 * gets loaded into the browser environment before the function <code>fn</code>
1.5 * is executed.
1.6 *
1.7 - * @param fn original function to call
1.8 + * @param fn original function to call (if <code>null</code> returns <code>null</code>)
1.9 * @param caller the class who wishes to define/call the function
1.10 * @param resource resources (accessible via {@link ClassLoader#getResource(java.lang.String)})
1.11 * with a <em>JavaScript</em> that is supposed to loaded into the browser
1.12 * environment
1.13 * @return function that ensures the script is loaded and then delegates
1.14 - * to <code>fn</code>
1.15 + * to <code>fn</code>. Returns <code>null</code> if the input <code>fn</code> is null
1.16 * @since 0.7
1.17 */
1.18 public static Fn preload(final Fn fn, final Class<?> caller, final String resource) {
1.19 + if (fn == null) {
1.20 + return null;
1.21 + }
1.22 return new Fn(fn.presenter()) {
1.23 @Override
1.24 public Object invoke(Object thiz, Object... args) throws Exception {
1.25 - final Presenter p = FnContext.currentPresenter(false);
1.26 - Set<Presenter> there = LOADED.get(resource);
1.27 - if (there == null) {
1.28 - there = new HashSet<Presenter>();
1.29 - LOADED.put(resource, there);
1.30 + Presenter p = presenter();
1.31 + if (p == null) {
1.32 + p = FnContext.currentPresenter(false);
1.33 }
1.34 - if (there.add(p)) {
1.35 - InputStream is = caller.getClassLoader().getResourceAsStream(resource);
1.36 - try {
1.37 - InputStreamReader r = new InputStreamReader(is, "UTF-8");
1.38 - p.loadScript(r);
1.39 - } finally {
1.40 - is.close();
1.41 + if (p != null) {
1.42 + Set<Presenter> there = LOADED.get(resource);
1.43 + if (there == null) {
1.44 + there = new HashSet<Presenter>();
1.45 + LOADED.put(resource, there);
1.46 + }
1.47 + if (there.add(p)) {
1.48 + InputStream is = caller.getClassLoader().getResourceAsStream(resource);
1.49 + try {
1.50 + InputStreamReader r = new InputStreamReader(is, "UTF-8");
1.51 + p.loadScript(r);
1.52 + } finally {
1.53 + is.close();
1.54 + }
1.55 }
1.56 }
1.57 return fn.invoke(thiz, args);
2.1 --- a/ko4j/src/main/java/org/netbeans/html/ko4j/Knockout.java Sun Feb 09 23:26:38 2014 +0100
2.2 +++ b/ko4j/src/main/java/org/netbeans/html/ko4j/Knockout.java Tue Feb 11 21:18:03 2014 +0100
2.3 @@ -59,13 +59,6 @@
2.4 */
2.5 @JavaScriptResource("knockout-2.2.1.js")
2.6 final class Knockout {
2.7 - static {
2.8 - loadKnockout();
2.9 - }
2.10 -
2.11 - @JavaScriptBody(args = { }, body = "")
2.12 - private static native void loadKnockout();
2.13 -
2.14 @JavaScriptBody(args = { "model", "prop" }, body =
2.15 "if (model) {\n"
2.16 + " var koProp = model[prop];\n"
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/ko4j/src/test/java/org/netbeans/html/ko4j/ReferenceKnockoutTest.java Tue Feb 11 21:18:03 2014 +0100
3.3 @@ -0,0 +1,63 @@
3.4 +/**
3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3.6 + *
3.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
3.8 + *
3.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
3.10 + * Other names may be trademarks of their respective owners.
3.11 + *
3.12 + * The contents of this file are subject to the terms of either the GNU
3.13 + * General Public License Version 2 only ("GPL") or the Common
3.14 + * Development and Distribution License("CDDL") (collectively, the
3.15 + * "License"). You may not use this file except in compliance with the
3.16 + * License. You can obtain a copy of the License at
3.17 + * http://www.netbeans.org/cddl-gplv2.html
3.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
3.19 + * specific language governing permissions and limitations under the
3.20 + * License. When distributing the software, include this License Header
3.21 + * Notice in each file and include the License file at
3.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
3.23 + * particular file as subject to the "Classpath" exception as provided
3.24 + * by Oracle in the GPL Version 2 section of the License file that
3.25 + * accompanied this code. If applicable, add the following below the
3.26 + * License Header, with the fields enclosed by brackets [] replaced by
3.27 + * your own identifying information:
3.28 + * "Portions Copyrighted [year] [name of copyright owner]"
3.29 + *
3.30 + * Contributor(s):
3.31 + *
3.32 + * The Original Software is NetBeans. The Initial Developer of the Original
3.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
3.34 + *
3.35 + * If you wish your version of this file to be governed by only the CDDL
3.36 + * or only the GPL Version 2, indicate your decision by adding
3.37 + * "[Contributor] elects to include this software in this distribution
3.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
3.39 + * single choice of license, a recipient has the option to distribute
3.40 + * your version of this file under either the CDDL, the GPL Version 2 or
3.41 + * to extend the choice of license to its licensees as provided above.
3.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
3.43 + * Version 2 license, then the option applies only if the new code is
3.44 + * made subject to such option by the copyright holder.
3.45 + */
3.46 +package org.netbeans.html.ko4j;
3.47 +
3.48 +import static org.testng.Assert.assertNull;
3.49 +import org.testng.annotations.Test;
3.50 +
3.51 +/**
3.52 + *
3.53 + * @author Jaroslav Tulach <jtulach@netbeans.org>
3.54 + */
3.55 +public class ReferenceKnockoutTest {
3.56 + @Test public void canLoadKnockout() {
3.57 + Object ret = null;
3.58 + try {
3.59 + ret = Knockout.toModel(null);
3.60 + } catch (NullPointerException ex) {
3.61 + // npe is OK as we don't have any
3.62 + // context for the
3.63 + }
3.64 + assertNull(ret, "Either null");
3.65 + }
3.66 +}