Skip to content

OpaqueToken Example

Nathan Walker edited this page Feb 22, 2017 · 2 revisions
// Define an Interface (will ensure web/ns classes implement similar behavior)...
// ie, \src\client\app\shared\core\interfaces\iuiservice.ts
export interface IUIService {
  alert(message: string);

}

// Define shared class...
// ie, \src\client\app\shared\core\services\uiservice.ts
export class UIService implements IUIService{
  alert(message: string) {};
}

// Define NativeScript Specific Implementation in...
// ie, \nativescript\src\mobile\core\services\native-uiservice.ts
import * as dialogsModule from "ui/dialogs";
export class NativeUIService implements IUIService {
  public alert(message: string) {
    //Using NS Dialogs. Or use any other NS plugin/library
    return dialogsModule.alert({
        title: "Message",
        okButtonText: "OK",
        message: message
    });
  }
}

// Set provider in...
// ie, \nativescript\src\native.module.ts
import { NativeUIService } from './mobile/core/index';
import { UIService } from './app/shared/core/services/ui.service';

   providers: [
        { provide: UIService, useClass: NativeUIService },
	

// Define Web Specific Implementation in...
// ie, \src\client\web-uiservice.ts
export class WebUIService  implements IUIService  {
  //Or use any other plugin/library
  public alert(message: string) {
	 //Use
    alert(message);

  }		
}

// Set provider in...
// ie, \src\client\web.module.ts
import { WebUIService} from './web-ui.service'
import { UIService } from './app/shared/core/services/ui.service';

  providers: [
    { provide: UIService, useClass: WebUIService },


// usage...
import { UIService } from '../../shared/core/index';

constructor(private ui: UIService)
{
	
}

Big thanks to @psmontte