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" } );
分享到:
相关推荐
在这个过程中,`mypy`会检查传递给API的参数类型,确保它们符合AWS Amplify API的要求。 2. 功能特性: - **类型安全**:`mypy-boto3-amplify`库提供了丰富的类型注解,确保在调用AWS Amplify服务时,参数和...
mypy_boto3_amplify则为后端开发者提供了一个强大的工具,让他们能够更好地管理和操作Amplify中的资源,如托管、API、身份验证等。通过这个库,开发者可以更安全地使用Amplify的API,确保每次调用都符合AWS的接口...
Create React App入门该项目是通过引导的。可用脚本在项目目录中,可以运行:yarn start 在开发模式下运行该应用程序。 打开在浏览器中查看它。 如果您进行编辑,则页面将重新加载。 您还将在控制台中看到任何棉绒...
扩增子 使用AWS AmplifyReact市场应用程序 ... 添加Amplify API: amplify add api :选择GraphQL,以默认配置使用Amazon Cognito用户池。 替换schema.graphql : type Market @model @searchable { id : ID ! name
Amazon Amplify是AWS(亚马逊网络服务)提供的一套工具和库,它允许开发者轻松地构建复杂的Web和移动应用,包括身份验证、API调用、数据存储和用户交互等功能。 描述中的"amplify-interactions-issue"很可能是指在...
Amplify提供了一整套工具和服务,包括身份验证、API访问、数据存储、预测、缓存等功能,使开发者能够快速构建可扩展的、高性能的应用程序。 在HTML方面,Amplify并不直接处理HTML的编写,但它可以通过JavaScript库...
通过`amplify api add`命令,开发者可以快速创建一个新的GraphQL API,并在JavaScript中使用`aws-amplify/api`库进行查询和突变操作。 **文件上传和下载**: 对于文件存储,Amplify提供了`Storage`类别,可以轻松...
可以使用`amplify add api`创建一个新的GraphQL或REST API,然后在Dart代码中调用这些API进行数据交互。 4. **身份验证和授权**: Amplify Authentication支持多种身份验证方案,如Cognito用户池,提供用户注册、...
AWS Amplify是由Amazon Web Services(AWS)提供的一款强大的前端开发框架,它简化了与AWS服务的集成,如身份验证、存储、数据库、API调用等。Reed Barger教授的Udemy课程“aws-amplify-reed-barger”旨在帮助开发者...
"官方文档"部分可能包含安装指南、快速入门教程、API参考以及常见问题解答。通过阅读这些文档,用户可以学习如何安装和配置Amplify Shader Editor,了解如何创建新的Shader,以及如何使用各种节点来实现特定的视觉...
**appSync-amplify-graphql API实践** 在现代Web开发中,GraphQL作为一种强大的查询语言,已经逐渐成为构建高效API的首选。Amazon AppSync是AWS提供的一项服务,它允许开发者使用GraphQL来实时同步数据,实现高效的...
基本的无服务器API 使用DynamoDB,API Gateway和AWS Lambda构建基本的无服务器API 入门 创建React项目,安装依赖项 $ npx create-react-app serverless-api $ cd serverless-api $ npm install aws-amplify 接下来...
5. GraphQL API与Amplify API:使用GraphQL查询语言来与后端数据交互,通过Amplify的API类别进行集成。 6. AWS AppSync:如果需要实时数据同步,可以考虑使用AppSync,它支持GraphQL并提供实时订阅功能。 7. 用户...
• 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 ...
• Custom Node API • Shader Templates • Shader Functions • Multi-window Support • Intuitive, familiar node interface • Extensive node library • Instancing Support • User Contributed Nodes and ...
例如,你可以利用它来配置Amplify的认证机制,集成API Gateway,或者设置数据存储。这大大简化了与AWS服务交互的过程,减少了手动配置的时间和错误。 在实际开发中,解压aws-cdk.aws-amplify-1.15.0.tar.gz文件后,...
运行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及更高...