apiway

2019-11-19 admin

apiway是什么

什么是apiway,Client side for Apiway framework

apiway使用教程帮助文档

Apiway - Client side

Server side

Getting started

# Install
$ npm install apiway --save                
// Require
import { Api, Resource, Store } from "apiway";

Hierarchy

EventEmitter 
|
|---> Api             
|
|---> Resource
|
|---> Store

EventEmitter docs

Api

Api provides an requests interface to actions of controllers server.

Methods

Api.connect( address[, options ] )

Open websocket connection to Apiway server, options:

  • aliveDelay - seconds between ping sendings, default: 0 (off)
Api.query( "controller.action", params )

Make query to controller action on server side with params, return Promise.

Api.send( event, data )

Send your custom event with data.

Api.disconnect()

Close connection.

Api.beforeReadyPromise( callback )

Set a method that returns a promise. Called after the connection and triggering ready/unready events. It can be used for pre-authentication before ready event.

Api.onReady( callback, context )  # call on every trigger
Api.oneReady( callback, context ) # call on first trigger

Attach callback with context to “ready” event.

Api.offReady( callback, context )

Detach callback with context from “ready” event.

Api.onUnready( callback, context )  # call on every trigger
Api.oneUnready( callback, context ) # call on first trigger

Attach callback with context to “unready” event.

Api.offUnready( callback, context )

Detach callback with context from “unready” event.

Example

Api
  .connect( "ws://localhost:3000", { aliveDelay: 5000 } )
  .beforeReadyPromise( function(){
    return Api.query( "Users.auth_by_token", { token: Cookie.get( "token" ) } );
  })
  .oneReady( function(){
    RouterRun();
  });
Api.query( "Messages.new", { text: "Hello world!" } )
  .then(  (e)=>{ console.log( "success" ) } )
  .catch( (e)=>{ console.log( "failure" ) } );

Resource

Resources provide access to relevant data and automatically synchronized when they change.

Create

let resource = new Resource( resourceName, params );

Create new resource with params.

Methods

resource.name

Getter to resource name.

resource.data

Getter to resource data.

resource.get( paramName )

Return value of resource param.

resource.set( { paramName: paramValue, paramName: paramValue } )

Set values of resource params.

resource.unset( paramName )

Remove param from resource params.

resource.onChange( callback, context )  # call on every trigger
resource.oneChange( callback, context ) # call on first trigger

Attach callback with context to “change” event.

resource.offChange( callback, context )

Detach callback with context from “change” event.

resource.onError( callback, context )  # call on every trigger
resource.oneError( callback, context ) # call on first trigger

Attach callback with context to “error” event.

resource.offError( callback, context )

Detach callback with context from “error” event.

Example

let currentUser = new Resource( "CurrentUser" );
currentUser
  .onChange( ( data )=>{ console.log( "Data of current user:", data ) })
  .onError( ( error )=>{ console.log( "Error", error ) });

// > Error auth_error

Api.query( "Users.auth_by_name", { name: "Bob" } );

// > Data of current user: {id: 1, name: "Bob"}

let messages = new Resource( "Messages", { limit: 3 } );
messages.onChange( ( data )=>{ console.log( "Messages:", data ) } );

// > Messages [{text: "Hello world"}, {text: "Hello world"}, {text: "Hello world"}]

messages.set({ limit: 2 });

// > Messages [{text: "Hello world"}, {text: "Hello world"}]

messages.get( "limit" );

// > 2

Store

Store is a simple object for the storage data of your application and access to them from different parts of the application.

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-79458.html

文章标题:apiway

回到顶部