`

amplify API

阅读更多
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" } );

分享到:
评论

相关推荐

    Python库 | mypy-boto3-amplify-1.12.22.0.tar.gz

    在这个过程中,`mypy`会检查传递给API的参数类型,确保它们符合AWS Amplify API的要求。 2. 功能特性: - **类型安全**:`mypy-boto3-amplify`库提供了丰富的类型注解,确保在调用AWS Amplify服务时,参数和...

    Python库 | mypy_boto3_amplify-1.17.47.0-py3-none-any.whl

    mypy_boto3_amplify则为后端开发者提供了一个强大的工具,让他们能够更好地管理和操作Amplify中的资源,如托管、API、身份验证等。通过这个库,开发者可以更安全地使用Amplify的API,确保每次调用都符合AWS的接口...

    amplify-crypto-app:熟悉Amazon API Gateway

    Create React App入门该项目是通过引导的。可用脚本在项目目录中,可以运行:yarn start 在开发模式下运行该应用程序。 打开在浏览器中查看它。 如果您进行编辑,则页面将重新加载。 您还将在控制台中看到任何棉绒...

    amplifyagora:AWS Amplify React市场

    扩增子 使用AWS AmplifyReact市场应用程序 ... 添加Amplify API: amplify add api :选择GraphQL,以默认配置使用Amazon Cognito用户池。 替换schema.graphql : type Market @model @searchable { id : ID ! name

    amplify-interactions-issue

    Amazon Amplify是AWS(亚马逊网络服务)提供的一套工具和库,它允许开发者轻松地构建复杂的Web和移动应用,包括身份验证、API调用、数据存储和用户交互等功能。 描述中的"amplify-interactions-issue"很可能是指在...

    amplify

    Amplify提供了一整套工具和服务,包括身份验证、API访问、数据存储、预测、缓存等功能,使开发者能够快速构建可扩展的、高性能的应用程序。 在HTML方面,Amplify并不直接处理HTML的编写,但它可以通过JavaScript库...

    AWS_Amplify_handson

    通过`amplify api add`命令,开发者可以快速创建一个新的GraphQL API,并在JavaScript中使用`aws-amplify/api`库进行查询和突变操作。 **文件上传和下载**: 对于文件存储,Amplify提供了`Storage`类别,可以轻松...

    amplify_codelab

    可以使用`amplify add api`创建一个新的GraphQL或REST API,然后在Dart代码中调用这些API进行数据交互。 4. **身份验证和授权**: Amplify Authentication支持多种身份验证方案,如Cognito用户池,提供用户注册、...

    aws-amplify-reed-barger:Reed Barger教授的AWS Amplify Udemy课程

    AWS Amplify是由Amazon Web Services(AWS)提供的一款强大的前端开发框架,它简化了与AWS服务的集成,如身份验证、存储、数据库、API调用等。Reed Barger教授的Udemy课程“aws-amplify-reed-barger”旨在帮助开发者...

    Amplify Shader Editor.zip

    "官方文档"部分可能包含安装指南、快速入门教程、API参考以及常见问题解答。通过阅读这些文档,用户可以学习如何安装和配置Amplify Shader Editor,了解如何创建新的Shader,以及如何使用各种节点来实现特定的视觉...

    appSync-amplify-graphql:appSync-amplify-graphql API实践

    **appSync-amplify-graphql API实践** 在现代Web开发中,GraphQL作为一种强大的查询语言,已经逐渐成为构建高效API的首选。Amazon AppSync是AWS提供的一项服务,它允许开发者使用GraphQL来实时同步数据,实现高效的...

    basic-serverless-api:使用AWS Amplify,Amazon API Gateway,AWS Lambda和Amazon DynamoDB构建API的基本完整堆栈示例

    基本的无服务器API 使用DynamoDB,API Gateway和AWS Lambda构建基本的无服务器API 入门 创建React项目,安装依赖项 $ npx create-react-app serverless-api $ cd serverless-api $ npm install aws-amplify 接下来...

    rn-netflix-app:在本系列中,我们将使用React Native和AWS Amplify从头开始构建Netflix移动应用程序。 对于从初学者到专业人士的所有人,这都是学习和实践React Native和AWS Amplify的混合移动开发的绝佳而有趣的机会。 让我们一起学习:open_book::light_bulb:

    5. GraphQL API与Amplify API:使用GraphQL查询语言来与后端数据交互,通过Amplify的API类别进行集成。 6. AWS AppSync:如果需要实时数据同步,可以考虑使用AppSync,它支持GraphQL并提供实时订阅功能。 7. 用户...

    Amplify Shader Editor 1.5.7

    • Custom Node API • Shader Templates • Shader Functions • Multi-window Support • Intuitive, familiar node interface • Extensive node library • Instancing Support • User Contributed Nodes and ...

    full-serverless-full-typescript

    使用Amplify CLI,我们可以创建API网关端点和Lambda函数。 CLI将指导您抛出配置API以便能够调用所需的步骤通过HTTP请求使用Lambda函数。 创建您的API后,我们将使用Amplify客户端与其进行交互。 发送请求到在React ...

    Amplify Shader Editor1.5.4 最新版

    • Custom Node API • Shader Templates • Shader Functions • Multi-window Support • Intuitive, familiar node interface • Extensive node library • Instancing Support • User Contributed Nodes and ...

    PyPI 官网下载 | aws-cdk.aws-amplify-1.15.0.tar.gz

    例如,你可以利用它来配置Amplify的认证机制,集成API Gateway,或者设置数据存储。这大大简化了与AWS服务交互的过程,减少了手动配置的时间和错误。 在实际开发中,解压aws-cdk.aws-amplify-1.15.0.tar.gz文件后,...

    Unity3D插件 Amplify Impostors 0.9.7.011

    "Amplify Impostors 0.9.7.011.txt" 文件可能包含了关于这个插件的详细文档,包括安装指南、使用教程、API 参考等信息。而 "Amplify Impostors 0.9.7.011.unitypackage" 文件则是这个插件的安装包,通过导入这个包到...

    aws-intro-amplify:带有Amplify Graphql APIAuth的Todo应用

    运行amplify add api 服务: Graphql API名称:默认 身份验证类型: Amazon Cognito User Pool 默认身份验证:默认 用户登录: Username 高级设置: No, I am done. 高级设置: No, I am done. 有一个架构: ...

Global site tag (gtag.js) - Google Analytics