amplify core
The AmplifyJS core library provides two methods (amplify.publish and amplify.subscribe). AmplifyJS provides methods to facilitate the Publish and Subscribe messaging pattern in your front-end application. The idea is that someone is broadcasting one or more messages (publishing) and someone else is listening to one or more messages (subscribing). By separating your logic out like this it allows for loose coupling of your components, which results in less brittle and more reusable code.
It is possible to implement the publish and subscribe model by using jQuery custom events, however, the AmplifyJS pub/sub component provides a slightly cleaner interface, prevents collisions between custom events and method names, and allows a priority to your messages.
Usage
amplify.subscribe( string topic, function callback )
amplify.subscribe( string topic, object context, function callback )
amplify.subscribe( string topic, function callback, number priority )
amplify.subscribe(
string topic, object context, function callback, number priority )
Subscribe to a message.
topic: Name of the message to subscribe to.
[context]: What this will be when the callback is invoked.
callback: Function to invoke when the message is published.
[priority]: Priority relative to other subscriptions for the same message. Lower values have higher priority. Default is 10.
Returning false from a subscription will prevent any additional subscriptions from being invoked and will cause amplify.publish to return false.
amplify.unsubscribe( string topic, function callback )
Remove a subscription.
topic: The topic being unsubscribed from.
callback: The callback that was originally subscribed.
amplify.publish( string topic, ... )
Publish a message.
topic: The name of the message to publish.
Any additional parameters will be passed to the subscriptions.
amplify.publish returns a boolean indicating whether any subscriptions returned false. The return value is true if none of the subscriptions returned false, and false otherwise. Note that only one subscription can return false because doing so will prevent additional subscriptions from being invoked.
Examples
Subscribe and publish with no data
Subscribing to a topic with no data allows a generic message to be published on the bus for any event (user or code related) that needs communicated but no data is needed along with the event.
amplify.subscribe( "nodataexample", function() {
alert( "nodataexample topic published!" );
});
//...
amplify.publish( "nodataexample" );
Subscribe and publish with data
The majority usage of a pub/sub system includes passing data from the publisher to any subscriptions listening to the topic.
amplify.subscribe( "dataexample", function( data ) {
alert( data.foo ); // bar
});
//...
amplify.publish( "dataexample", { foo: "bar" } );
You can choose to pass multiple parameters to any subscriber as well.
amplify.subscribe( "dataexample2", function( param1, param2 ) {
alert( param1 + param2 ); // barbaz
});
//...
amplify.publish( "dataexample2", "bar", "baz" );
Subscribe and publish with context and data
Often there is a need to operate within a context for a subscription callback. It can be a reasonable strategy to have the context be set to a jQuery object that will be used inside of the subscription, or even a native DOM element.
Note: the following example assumes jQuery is already loaded on the page, and assumes at least one paragraph exists within the body of the page.
amplify.subscribe( "datacontextexample", $( "p:first" ), function( data ) {
this.text( data.exampleText ); // first p element would have "foo bar baz" as text
});
//...
amplify.publish( "datacontextexample", { exampleText: "foo bar baz" } );
Subscribe to a topic with high priority
Subscribing to a topic with high priority can be useful as an error handler or anytime data may need to be checked or augmented before proceeding.
amplify.subscribe( "priorityexample", function( data ) {
alert( data.foo );
});
amplify.subscribe( "priorityexample", function( data ) {
if ( data.foo === "oops" ) {
return false;
}
}, 1 );
//...
amplify.publish( "priorityexample", { foo: "bar" } );
amplify.publish( "priorityexample", { foo: "oops" } );
分享到:
相关推荐
Create React App入门该项目是通过引导的。可用脚本在项目目录中,可以运行:yarn start 在开发模式下运行该应用程序。 打开在浏览器中查看它。 如果您进行编辑,则页面将重新加载。 您还将在控制台中看到任何棉绒...
扩增子 使用AWS AmplifyReact市场应用程序 ... 添加Amplify API: amplify add api :选择GraphQL,以默认配置使用Amazon Cognito用户池。 替换schema.graphql : type Market @model @searchable { id : ID ! name
基本的无服务器API 使用DynamoDB,API Gateway和AWS Lambda构建基本的无服务器API 入门 创建React项目,安装依赖项 $ npx create-react-app serverless-api $ cd serverless-api $ npm install aws-amplify 接下来...
• Custom Node API • Shader Templates • Shader Functions • Multi-window Support • Intuitive, familiar node interface • Extensive node library • Instancing Support • User Contributed Nodes and ...
• Custom Node API • Shader Templates • Shader Functions • Multi-window Support • Intuitive, familiar node interface • Extensive node library • Instancing Support • User Contributed Nodes and ...
使用Amplify CLI,我们可以创建API网关端点和Lambda函数。 CLI将指导您抛出配置API以便能够调用所需的步骤通过HTTP请求使用Lambda函数。 创建您的API后,我们将使用Amplify客户端与其进行交互。 发送请求到在React ...
appSync-amplify-graphql:appSync-amplify-graphql API实践
运行amplify add api 服务: Graphql API名称:默认 身份验证类型: Amazon Cognito User Pool 默认身份验证:默认 用户登录: Username 高级设置: No, I am done. 高级设置: No, I am done. 有一个架构: ...
•Amplify Shader Editor通过高达v7.2.x的模板支持HD,Lightweight和Universal RP。 •Legacy HD和Lightweight SRP v3.xx / v4.xx / v5.xx模板也通过Legacy软件包提供。 •通过自定义RT模板支持在Unity 2017及更高...
AWS Amplify Vue入门 集成了VueJs入门应用程序。 请将问题提交到存储库。 入门 克隆项目并安装依赖项 ...$ amplify add api $ > GraphQL ... $ > Amazon Cognito User Pool $ Do you have an annotat
添加对数据存储的支持,它会为您创建API(此后无需键入amplify add api ) npx amplify-app 添加我们的GraphQL模式 echo " enum PostStatus { ACTIVE INACTIVE } type Post @model { id: ID! title: String! ...
该项目提供了一个瘦包装程序脚本amplifylocal用于针对提供的本地API使用库。 快速安装 amplifylocal命令行发布为: $ npm install -g amplify-js-local @aws-amplify/cli ... 注意:依赖项@aws-amplify/cli需要...
适用于iOS的Amplify AWS Amplify在不同类别的云操作中提供了一个声明式且易于使用的界面。 AWS Amplify可与任何基于JavaScript的前端工作流以及适用于移动开发人员的React Native完美配合。 我们的默认实现可与...
模拟-放大-身份验证一些用于身份验证的AWS Amplify SDK的模拟实现为什么?...支持的API signUp signIn currentSession confirmSignUp signOut resendSignUp执照麻省理工学院。 参见版权所有fourTheorem Ltd.2019
此存储库是将Amplify客户端库与Terraform结合使用(并且对Lambda函数使用无服务器)而不是Amplify CLI和控制台的概念证明。 请参阅我的博客以了解原因: : 先决条件 安装以下工具: 设置AWS API凭证:AWS aws ...
amplify_blog 一个小型博客应用程序〜入门该项目是Flutter应用程序的起点。 如果这是您的第一个Flutter项目,那么有一些资源可以帮助您入门:要获得Flutter入门方面的帮助,请查看我们的,其中提供了教程,示例,...
Amplify Central Agent SDK提供了API和实用程序,开发人员可以使用它们来构建基于Golang的应用程序,以发现远程API网关(例如AWS,Azure,Axway API Manager等)上托管的API,并在Amplify Central中将其表示形式...
功能/ API:想要创建用户身份验证体验的开发人员的API和构件。(即将推出):轻松收集应用程序的分析数据。 Google Analytics(分析)数据包括您要在应用中跟踪的用户会话和其他自定义事件。 (即将推出):提供HTTP...
欢迎使用AWS Amplify进行Amp'd Up ...支架:创建GraphQL API端点运行命令amplify add api并将其设置为GraphQL-Todo模式解释发生了什么... 显示生成的ToDo Schema工件待办事项:更多详细信息... 运行命令amplify push ap