Skip to content

Commit

Permalink
feat(plugin): proxy plugin properties
Browse files Browse the repository at this point in the history
Rename RequiresPlugin decorator to CordovaProperty and replace getter so
we can forward property access to the underlying plugin property.
  • Loading branch information
tlancina committed Mar 4, 2016
1 parent f360827 commit fc54fef
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
10 changes: 7 additions & 3 deletions src/plugins/apprate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {Plugin, Cordova} from './plugin';
import {Plugin, Cordova, CordovaProperty} from './plugin';

declare var window;

/**
* The AppRate plugin makes it easy to prompt the user to rate your app, either now, later, or never.
Expand Down Expand Up @@ -43,8 +45,10 @@ export class AppRate {
* customLocale {Object} null - custom locale object
* @type {{}}
*/
@Cordova()
static preferences = {};
@CordovaProperty
static get preferences(){
return window.AppRate.preferences;
}

/**
* Prompts the user for rating
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/device.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Plugin, RequiresPlugin} from './plugin';
import {Plugin, CordovaProperty} from './plugin';

declare var window: {
device: Device
Expand Down Expand Up @@ -49,8 +49,8 @@ export class Device {
*
* @returns {Object} The device object.
*/
@RequiresPlugin
static getDevice() {
@CordovaProperty
static get device() {
return window.device;
}
}
10 changes: 5 additions & 5 deletions src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,20 @@ export function Cordova(opts:any = {}) {
/**
* Before calling the original method, ensure Cordova and the plugin are installed.
*/
export function RequiresPlugin(target: Function, key: string, descriptor: TypedPropertyDescriptor<any>) {
let originalMethod = descriptor.value;
export function CordovaProperty(target: Function, key: string, descriptor: TypedPropertyDescriptor<any>) {
let originalMethod = descriptor.get;

descriptor.value = function(...args: any[]) {
descriptor.get = function(...args: any[]) {
// console.log('Calling', this);
if(!window.cordova) {
cordovaWarn(this.name, null);
return;
return {};
}

let pluginInstance = getPlugin(this.pluginRef);
if(!pluginInstance) {
pluginWarn(this, key);
return;
return {};
}
return originalMethod.apply(this, args);
}
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/statusbar.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Plugin, Cordova, RequiresPlugin} from './plugin';
import {Plugin, Cordova, CordovaProperty} from './plugin';

declare var window;

Expand Down Expand Up @@ -102,8 +102,8 @@ export class StatusBar {
/**
* Whether the StatusBar is currently visible or not.
*/
@RequiresPlugin
static isVisible() {
@CordovaProperty
static get isVisible() {
return window.StatusBar.isVisible;
}
}

0 comments on commit fc54fef

Please sign in to comment.