mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
application module events added
This commit is contained in:
@ -3,6 +3,10 @@ import definition = require("application");
|
||||
import fs = require("file-system");
|
||||
import fileSystemAccess = require("file-system/file-system-access");
|
||||
import styleScope = require("ui/styling/style-scope");
|
||||
import observable = require("data/observable");
|
||||
|
||||
var events = new observable.Observable();
|
||||
require("utils/module-merge").merge(events, exports);
|
||||
|
||||
export var cssFile: string = "app.css"
|
||||
|
||||
|
@ -43,6 +43,9 @@ var initEvents = function () {
|
||||
if (exports.onExit) {
|
||||
exports.onExit();
|
||||
}
|
||||
|
||||
exports.notify({ eventName: "onExit", object: androidApp, android: activity, ios: undefined });
|
||||
|
||||
androidApp.startActivity = undefined;
|
||||
}
|
||||
|
||||
@ -58,6 +61,9 @@ var initEvents = function () {
|
||||
if (exports.onSuspend) {
|
||||
exports.onSuspend();
|
||||
}
|
||||
|
||||
exports.notify({ eventName: "onSuspend", object: androidApp, android: activity, ios: undefined });
|
||||
|
||||
}
|
||||
|
||||
if (androidApp.onActivityPaused) {
|
||||
@ -69,6 +75,9 @@ var initEvents = function () {
|
||||
if (exports.onResume) {
|
||||
exports.onResume();
|
||||
}
|
||||
|
||||
exports.notify({ eventName: "onResume", object: androidApp, android: activity, ios: undefined });
|
||||
|
||||
}
|
||||
|
||||
if (androidApp.onActivityResumed) {
|
||||
@ -183,6 +192,8 @@ class AndroidApplication implements dts.AndroidApplication {
|
||||
exports.onLaunch(intent);
|
||||
}
|
||||
|
||||
exports.notify({ eventName: "onLaunch", object: this, android: intent, ios: undefined });
|
||||
|
||||
/* In the onLaunch event we expect the following setup, which ensures a root frame:
|
||||
* var frame = require("ui/frame");
|
||||
* var rootFrame = new frame.Frame();
|
||||
@ -224,6 +235,8 @@ global.__onUncaughtError = function (error: Error) {
|
||||
}
|
||||
|
||||
exports.onUncaughtError(nsError);
|
||||
|
||||
exports.notify({ eventName: "onUncaughtError", object: appModule.android, android: error, ios: undefined });
|
||||
}
|
||||
|
||||
exports.start = function () {
|
||||
|
66
application/application.d.ts
vendored
66
application/application.d.ts
vendored
@ -3,6 +3,7 @@
|
||||
*/
|
||||
declare module "application" {
|
||||
import cssSelector = require("ui/styling/css-selector");
|
||||
import observable = require("data/observable");
|
||||
|
||||
/**
|
||||
* An extended JavaScript Error which will have the nativeError property initialized in case the error is caused by executing platform-specific code.
|
||||
@ -14,6 +15,21 @@ declare module "application" {
|
||||
nativeError: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event data containing information for the application events.
|
||||
*/
|
||||
export interface ApplicationEventData extends observable.EventData {
|
||||
/**
|
||||
* Gets the native iOS event arguments. Valid only when running on iOS.
|
||||
*/
|
||||
ios: any;
|
||||
|
||||
/**
|
||||
* Gets the native Android event arguments. Valid only when running on Android.
|
||||
*/
|
||||
android: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* The main page path (without the file extension) for the application starting from the application root.
|
||||
* For example if you have page called "main.js" in a folder called "subFolder" and your root folder is "app" you can specify mainModule like this:
|
||||
@ -82,6 +98,56 @@ declare module "application" {
|
||||
*/
|
||||
export function onLowMemory();
|
||||
|
||||
/**
|
||||
* A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
|
||||
* @param eventNames - String corresponding to events (e.g. "onLaunch"). Optionally could be used more events separated by `,` (e.g. "onLaunch", "onSuspend").
|
||||
* @param callback - Callback function which will be executed when event is raised.
|
||||
* @param thisArg - An optional parameter which will be used as `this` context for callback execution.
|
||||
*/
|
||||
export function on(eventNames: string, callback: (data: any) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* Notifies all the registered listeners for the event provided in the data.eventName.
|
||||
* @param data The data associated with the event.
|
||||
*/
|
||||
export function notify(data: ApplicationEventData): void;
|
||||
|
||||
/**
|
||||
* Checks whether a listener is registered for the specified event name.
|
||||
* @param eventName The name of the event to check for.
|
||||
*/
|
||||
export function hasListeners(eventName: string): boolean;
|
||||
|
||||
/**
|
||||
* This event is raised on application launch.
|
||||
*/
|
||||
export function on(event: "onLaunch", callback: (args: any) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised when an uncaught error occurs while the application is running.
|
||||
*/
|
||||
export function on(event: "onUncaughtError", callback: (args: any) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised when the Application is suspended.
|
||||
*/
|
||||
export function on(event: "onSuspend", callback: (args: any) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised when the Application is resumed after it has been suspended.
|
||||
*/
|
||||
export function on(event: "onResume", callback: (args: any) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised when the Application is about to exit.
|
||||
*/
|
||||
export function on(event: "onExit", callback: (args: any) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised when there is low memory on the target device.
|
||||
*/
|
||||
export function on(event: "onLowMemory", callback: (args: any) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This is the Android-specific application object instance.
|
||||
* Encapsulates methods and properties specific to the Android platform.
|
||||
|
@ -50,6 +50,8 @@ class TNSAppDelegate extends UIResponder implements UIApplicationDelegate {
|
||||
exports.onLaunch();
|
||||
}
|
||||
|
||||
exports.notify({ eventName: "onLaunch", object: this, android: undefined, ios: launchOptions });
|
||||
|
||||
var topFrame = frame.topmost();
|
||||
if (!topFrame) {
|
||||
if (mainModule) {
|
||||
@ -74,6 +76,8 @@ class TNSAppDelegate extends UIResponder implements UIApplicationDelegate {
|
||||
if (exports.onResume) {
|
||||
exports.onResume();
|
||||
}
|
||||
|
||||
exports.notify({ eventName: "onResume", object: this, android: undefined, ios: application });
|
||||
}
|
||||
|
||||
applicationWillResignActive(application: UIApplication) {
|
||||
@ -84,6 +88,8 @@ class TNSAppDelegate extends UIResponder implements UIApplicationDelegate {
|
||||
if (exports.onSuspend) {
|
||||
exports.onSuspend();
|
||||
}
|
||||
|
||||
exports.notify({ eventName: "onSuspend", object: this, android: undefined, ios: application });
|
||||
}
|
||||
|
||||
applicationWillEnterForeground(application: UIApplication) {
|
||||
@ -94,12 +100,16 @@ class TNSAppDelegate extends UIResponder implements UIApplicationDelegate {
|
||||
if (exports.onExit) {
|
||||
exports.onExit();
|
||||
}
|
||||
|
||||
exports.notify({ eventName: "onExit", object: this, android: undefined, ios: application });
|
||||
}
|
||||
|
||||
applicationDidReceiveMemoryWarning(application: UIApplication) {
|
||||
if (exports.onLowMemory) {
|
||||
exports.onLowMemory();
|
||||
}
|
||||
|
||||
exports.notify({ eventName: "onLowMemory", object: this, android: undefined, ios: application });
|
||||
}
|
||||
|
||||
applicationOpenURLSourceApplicationAnnotation(application: UIApplication, url: NSURL, sourceApplication: string, annotation: any): boolean {
|
||||
@ -147,5 +157,7 @@ exports.start = function () {
|
||||
}
|
||||
|
||||
exports.onUncaughtError(error);
|
||||
|
||||
definition.notify({ eventName: "onUncaughtError", object: <any>definition.ios, android: undefined, ios: error });
|
||||
}
|
||||
}
|
@ -1,3 +1,28 @@
|
||||
import application = require("application");
|
||||
application.mainModule = "app/mainPage";
|
||||
|
||||
application.on("onLaunch", function (args) {
|
||||
console.log("onLaunch: " + args);
|
||||
});
|
||||
|
||||
application.on("onUncaughtError", function (args) {
|
||||
console.log("onUncaughtError: " + args);
|
||||
});
|
||||
|
||||
application.on("onSuspend", function (args) {
|
||||
console.log("onSuspend: " + args);
|
||||
});
|
||||
|
||||
application.on("onResume", function (args) {
|
||||
console.log("onResume: " + args);
|
||||
});
|
||||
|
||||
application.on("onExit", function (args) {
|
||||
console.log("onExit: " + args);
|
||||
});
|
||||
|
||||
application.on("onLowMemory", function (args) {
|
||||
console.log("onLowMemory: " + args);
|
||||
});
|
||||
|
||||
application.start();
|
||||
|
@ -494,6 +494,8 @@ var NativeActivity = {
|
||||
gc();
|
||||
java.lang.System.gc();
|
||||
this.super.onLowMemory();
|
||||
|
||||
application.notify(<application.ApplicationEventData>{ eventName: "onLowMemory", object: this, android: undefined, ios: undefined });
|
||||
},
|
||||
|
||||
onTrimMemory: function (level: number) {
|
||||
|
Reference in New Issue
Block a user