React Native 应用程序中的 JSONStore

improve this page | report issue


先决条件

跳转至:

添加 JSONStore

要向 React Native 应用程序添加 JSONStore 插件:

  1. 打开命令行窗口并浏览至 React Native 项目文件夹。
  2. 运行以下命令:
     npm install react-native-ibm-mobilefirst-jsonstore --save
    

基本用法

创建新 JSONStore 集合

  1. 我们使用 JSONStoreCollection 类创建 JSONStore 的实例。我们还可以将其他配置设置为此新创建的 JSONStore 集合(例如,设置搜索字段)。
  2. 要开始与现有的 JSONStore 集合交互(例如:添加或移除数据),我们需要打开集合。我们使用 openCollections() API 来执行此操作。
    var collection = new JSONStoreCollection('people');
    WLJSONStore.openCollections(['people'])
    .then(res => {
    	// handle success
    }).catch(err => {
    	// handle failure
    });
    

添加

使用 addData() API 将 JSON 数据存储在集合中。

var data = { "name": "John", age: 28 };
var collection = new JSONStoreCollection('people');
collection.addData(data)
.then(res => {
  // handle success
}).catch(err => {
  // handle failure
});

您可以使用该 API 添加单个 JSON 对象或 JSON 对象数组。

查找

  1. 使用 find 来通过查询查找集合中的文档。
  2. 使用 findAllDocuments() API 来检索集合中的所有文档。
  3. 使用 findDocumentById()findDocumentsById() API 来根据文档唯一标识进行搜索。
  4. 使用 findDocuments() API 来查询集合。对于查询,您可以使用 JSONStoreQueryPart 类对象来过滤数据。

JSONStoreQueryPart 对象数组作为参数传递至 findDocuments API。

var collection = new JSONStoreCollection('people');
var query = new JSONStoreQueryPart();
query.addEqual("name", "John");
collection.findDocuments([query])
.then(res => {
	// handle success
}).catch(err => {
	// handle failure
});

除去

使用 remove 以删除集合中的文档。

var id = 1; // for example
var collection = new JSONStoreCollection('people');
collection.removeDocumentById(id)
.then(res => {
	// handle success
}).catch(err => {
	// handle failure     
});

除去集合

使用 removeCollection 来删除集合中存储的所有文档。 此操作类似于数据库术语中的删除表。

var collection = new JSONStoreCollection('people');
collection.removeCollection()
.then(res => {
	// handle success
}).catch(err => {
	// handle failure
});

IBM MobileFirst JSONStore 的样本应用程序

此处下载样本。

运行样本

在样本的根目录中,运行以下命令,这将安装所有项目依赖关系:

npm install

注:确保您的 mfpclient.propertiesmfpclient.plist 指向正确的 MobileFirst Server。

  1. 注册应用程序。转至 android 目录并运行以下命令:
     mfpdev app register
    
  2. 配置应用程序。(仅针对 Android)
    • 从 React Native 项目根目录打开 android/app/src/main/AndroidManifest.xml 文件。
      <manifest> 标记添加以下行:
      xmlns:tools="http://schemas.android.com/tools"
      <application> 标记添加以下行:
      tools:replace="android:allowBackup"

      react-native-ibm-mobilefirst 库需要执行此步骤。

    • 从 React Native 项目根目录打开 android/app/build.gradle 文件。
      android {} 内添加以下代码:

      packagingOptions{
      	exclude 'META-INF/ASL2.0'
      }
      

      react-native-ibm-mobilefirst-jsonstore 库需要执行此步骤。

  3. 运行应用程序。返回至根目录并浏览至 iOS 目录,然后运行以下命令:mfpdev app register

我们现在可运行应用程序。 要在 Android 上运行,请执行以下命令:

react-native run-android
Inclusive terminology note: The Mobile First Platform team is making changes to support the IBM® initiative to replace racially biased and other discriminatory language in our code and content with more inclusive language. While IBM values the use of inclusive language, terms that are outside of IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the documentation to reflect those changes.
Last modified on June 01, 2020