且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

IBM Worklight - 如何在多页面应用程序中重用函数

更新时间:2022-12-21 11:03:08

你没有在WHICH .js文件中提到你有放置此功能也不如何在页面之间导航这是至关重要的信息。将此信息添加到您的问题中。

You do not mention in WHICH .js file you have placed this function nor how do you navigate between pages. This is crucial information. Add this information to your question.

Worklight 6.1 应用程序 - 的上下文中,不知道任何其他信息你的项目 - 我的建议是将此函数放在 common \ js \main.js 文件中。此 main.js 文件在Worklight应用程序的 index.html HEAD 中引用/ code> file。

In the context of a Worklight 6.1 application - without knowing anything else about your project - my suggestion is to place this function in the common\js\main.js file. This main.js file is referenced in the HEAD of your Worklight application's index.html file.

要重复使用此功能,您可以通过替换 index.html 文件使用jQuery的加载 API方法。这样,无论您要加载哪个页面,该函数始终可用。

To reuse this function, you can then for example navigate between "pages" by replacing the contents of a DIV within the index.html file by using jQuery's load API method. This way the function is always available no matter which "page" you are loading.

这是实现页面加载的两个示例项目:

These are two sample projects that implement "page" loading:

  • JQM_multipage_load_changePage.zip - uses either .load or .changePage
  • JQM_multipage_changePage_pageshow.zip - uses .changePage and .pageShow

编辑:在回答的评论中使用提供的演示项目,我在其中更改了以下内容:

using the provided demo project in the comments to this answer, I have changed the following in it:


  1. 更改此:

  1. Change this:


$(document).on("pageshow", "#one", function( event ) {
    alert( "page 1 loaded" );
});

$(document).on("pageshow", "#two", function( event ) {
    alert( "page 2 loaded" );
});


到此:


$(document).on("pageshow", "#one", function( event ) {
    alert( "page 1 loaded" );
    stopCount();
    doTimer();
});

$(document).on("pageshow", "#two", function( event ) {
    alert( "page 2 loaded" );
    stopCount();
    doTimer();
});


验证 doTimer()确实调用了code>,你可以在 doTimer()里面添加一个警告,比如 alert(在doTimer中);

To verify doTimer() is indeed called, you can add an alert inside doTimer(), like alert ("in doTimer");

要验证 stopCount()确实已调用,您可以在 doTimer(),如 alert(在stopCount中);

To verify stopCount() is indeed called, you can add an alert inside doTimer(), like alert ("in stopCount");

从HTML中删除 onload

来自:


< div id =level-one-2data-role =pageonload =doTimer()>

收件人:


< div id =level-one-2data-role =page>