打开另一个.JS文件中定义的Realm-JS [英] Open Realm-JS Defined in Another .JS File

查看:160
本文介绍了打开另一个.JS文件中定义的Realm-JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 react-native 中使用 realm-js ,所以我所做的就是在我的操作文件夹中创建了一个realm-js类,如 LoginActions.js ,它将在用户登录后写入数据.

I am trying to use realm-js in react-native so what I did is created a class of realm-js in my action folder as in LoginActions.js, which will write the data after user logs in.

我的问题是如何将相同的架构打开到另一个.js文件中?

LoginActions.js中定义的Realm-js:

Realm-js Defined in LoginActions.js:

class Login {
  get token() {
    return this.token;
  }
}

Login.schema = {
  name: 'Login',
  primaryKey: 'id',
  properties: {
    id: 'int',
    token: 'string',
  }
};

然后,使用一些功能在此模式下打开要写入和更新的领域.

Then I Open the Realm to Write and Update in this Schema with Some of my functions.

赞:

// If Login User Success
Realm.open({ schema: [Login] })
.then(realm => {
  realm.write(() => {
    realm.create('Login', { id: 1, token });
  });
});

现在,如果要将此模式打开到另一个.js文件中.我将如何打开它? 我想要的主要格式类似于Loading.js:

Now If want to Open this Schema into Another .js File. How will I Open it? The Main Format that I want is like in Loading.js:

import ????? from ????;

OR

Realm.open(????????)
.then(realm => {
  realm.write(() => {
    const isUser = realm.create('Login', { id: 1, token }, true);
    if(isUser.length == 1) {
        // User Logs In Directly to Main Screen
    } else {
        // Show them Login Screen
    }
  });
});

推荐答案

您可以保存Realm实例,并稍后在另一个.js文件中使用它.

You could save the Realm instance and use it later in the other .js file.

即使您在应用中使用Redux,这也是我建议使用的方式.

This is the way I recommend to use, even if you are using Redux in your app.

模式和领域创建发生在realm.js文件中,该文件随后导入到代码中您需要的任何位置.

The schema and the realm creation happen in a realm.js file that is later imported anywhere you need in your code.

realm.js文件看起来像(带有2个示例模式):

The realm.js file looks like (with 2 example schemas):

import Realm from 'realm'; 

class Favorite extends Realm.Object {}
Favorite.schema = {
  name: 'Favorite',
  primaryKey: 'id', 
  properties: {
    id:     'string', 
    type:     'string', 
    favorite: 'bool', 
    alert: 'bool', 
    tag:     'string', 
    artistId: 'int'  
  }
};

class Friend extends Realm.Object {}
Friend.schema = {
  name: 'Friend',
  primaryKey: 'fbId',
  properties: {
    name: 'string',
    fbId:'string',  
    email: 'string',
    foto: 'string',
    concerts: 'int[]',
    artists: 'int[]'
  }
};

var schemasArray = [
  Favorite, 
  Friend

export default new Realm({schema: schemasArray});

然后从您应用程序中的任何组件或文件中只需执行以下操作:

then from any component or file in your app you can simply do:

import realm from './realm'

并按照领域文档中的说明使用它:

and use it as in the realm documentation:

try {
  if(realm){
    realm.write(() => {
      //delete all objects first
      var allFavorites = realm.objects('Favorite')
      realm.delete(allFavorites)
  } catch (e) {
    console.log(`Error deleting favorites: ${schema} - ${e}`);
    reject()
  }

Redux方式

如果您正在使用Redux,则可以先将领域实例保存到应用程序状态,然后再通过状态道具将其传递给任何组件. 最后的结果非常相似,但是您添加了更多样板代码以将领域保存到商店中.

Redux way

If you are using Redux, you could save the realm instance to the application state first to them pass it to any component via props from the state. At the end the result is very similar, but you add more boiler plate code to save realm into the store.

Realm.open({ schema: [Login] })
.then(realm => {
  this.saveRealm( realm )
});

动作

saveRealm是一个动作:

Actions

saveRealm is an action:

export function saveRealm( realm ) {
  return {
    type: types.SAVE_REALM,
    realm
  }
}

减速器

将在化简器中处理的

Reducer

that will get handled in the reducer:

export default function reducer(state = initialState, action = {}) {

  switch (action.type) {
    case types.SAVE_REALM:
      return Object.assign({}, state, {
        realm: action.realm
      });

      //other actions
    default:
       return state;
  }

文件2

最后,在其他文件.js中,只需使用保存的领域实例,而无需再次打开它.

File 2

Finally in other file .js just use the saved realm instance, without having to open it again.

this.props.realm.write(() => {
    const isUser = this.props.realm.create('Login', { id: 1, token }, true);
   //rest of your code...
  });

您将需要照常使用mapStateToProps:

You'll need to use mapStateToProps as usual:

function mapStateToProps(state) {
  return {
    realm: state.realm,

将不带Redux"作为推荐选项.向其中添加更多代码示例代码.

Make the "Without Redux" the recommended option. Add more code example code to it.

这篇关于打开另一个.JS文件中定义的Realm-JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆