2017-01-02 57 views
1

我正在使用redux-orm來創建用於規範化和非規範化的模型。我發現,當我創建一個模式,我得到的錯誤:在redux-orm模式實例上使用reducer方法

Uncaught Error: Schema has been renamed to ORM. Please import ORM instead of Schema from Redux-ORM 

我用兩個0.90-rc1這是默認安裝以及`0.8.4

這段代碼運行時版本:

import { Schema } from 'redux-orm' 
import Todo from './Todo' 
import Tag from './Tag' 
import User from './User' 

const schema = new Schema() 
schema.register(Todo, Tag, User) 

export default schema 

但是我發現redux-orm中的模式文檔和代碼仍然存在。

如果我從

import { Schema } from 'redux-orm' 

切換到

import { ORM as Schema } from 'redux-orm' 

代碼的作品,但我得到一個錯誤指定reducer方法沒有在這裏定義:

import { schema } from './models' 

console.log(schema) 

const rootReducer = combineReducers({ 
    orm: schema.reducer(), 
    selectedUserId: selectedUserIdReducer 
}) 

大多數代碼是基於primer here

我的模型是這樣的:

ValidatingModel.js

import { PropTypes } from 'react' 
import { Model } from 'redux-orm' 
import propTypesMixin from 'redux-orm-proptypes' 

const ValidatingModel = propTypesMixin(Model) 

export default ValidatingModel 

Todo.js

import { fk, many } from 'redux-orm' 
import { PropTypes } from 'react' 
import ValidatingModel from './ValidatingModel' 
import User from './User' 
import Tag from './Tag' 
import { CREATE_TODO, MARK_DONE, DELETE_TODO, ADD_TAG_TO_TODO, REMOVE_TAG_FROM_TODO } from '../actionTypes' 
export default class Todo extends ValidatingModel { 
    static reducer (state, action, Todo, session) { 
    const { payload, type } = action 
    switch (type) { 
     case CREATE_TODO: 
     const tagIds = payload.tags.split(',').map(str => str.trim()) 
     const props = Object.assign({}, payload, { tags: tagIds }) 
     Todo.create(props) 
     break 
     case MARK_DONE: 
     Todo.withId(payload).set({ done: true }) 
     break 
     case DELETE_TODO: 
     Todo.withId(payload).delete() 
     break 
     case ADD_TAG_TO_TODO: 
     Todo.withId(payload.todo).tags.add(payload.tag) 
     break 
     case REMOVE_TAG_FROM_TODO: 
     Todo.withId(payload.todo).tags.remove(payload.tag) 
     break 
    } 
    } 
} 

Todo.modelName = 'Todo' 

Todo.propTypes = { 
    id: PropTypes.number, 
    text: PropTypes.string.isRequired, 
    done: PropTypes.bool.isRequired, 
    user: PropTypes.oneOf([PropTypes.instanceOf(User), PropTypes.number]), 
    tags: PropTypes.arrayOf(PropTypes.oneOf([ 
    PropTypes.number, 
    PropTypes.instanceOf(Tag) 
    ])) 
} 

Todo.defaultProps = { 
    done: false 
} 

Todo.fields = { 
    user: fk('User', 'todos'), 
    tags: many('Tag', 'todos') 
} 

Tag.js

import ValidatingModel from './ValidatingModel' 
import { PropTypes } from 'react' 
import { CREATE_TODO, ADD_TAG_TO_TODO } from '../actionTypes' 

export default class Tag extends ValidatingModel { 
    static reducer (state, action, Tag, session) { 
    const { payload, type } = action 
    switch (type) { 
     case CREATE_TODO: 
     const tags = payload.tags.split(',') 
     const trimmed = tags.map(name => name.trim()) 
     trimmed.forEach(name => Tag.create(name)) 
     break 
     case ADD_TAG_TO_TODO: 
     if (!Tag.filter({ name: payload.tag }).exists()) { 
      Tag.create({ name: payload.tag }) 
     } 
     break 
    } 
    } 
} 

Tag.modelName = 'Tag' 

Tag.backend = { 
    idAttribute: 'name' 
} 

Tag.propTypes = { 
    name: PropTypes.string 
} 

這是用戶模型,我添加了一個空操作減速看到@squiroid原來的答案

import ValidatingModel from './ValidatingModel' 
import { PropTypes } from 'react' 

export default class User extends ValidatingModel { 
    static reducer (state, action, User, session) { 
    return state 
    } 
} 

User.modelName = 'User' 

User.propTypes = { 
    id: PropTypes.number, 
    name: PropTypes.string 
} 

回答

2

首先,我需要使用ORM代替Schema。所以我改變了進口爲Schema到:

import { ORM as Schema } from 'redux-orm' 

其次,我不得不減速的簽名更改爲:從簽名文檔中

static reducer (action, SessionSpecificModel, session) 

the redux-orm docs have been updated, the second argument is not the state of the reducer but a session specific model.

static reducer (state, action, Model, session) 

第三,我必須更改todo中的代碼才能使用爲了調用mapTodoTag實例的列表上3210或toModelArray

return orm.Todo.filter({ user: userId }).toModelArray().map(todo => { 
    const obj = Object.assign({}, todo.ref) 
    obj.tags = todo.tags.toRefArray().map(tag => tag.name) 
    return obj 
}) 

第四,我不得不從session實例解析Model類。

我仍然發現的問題與創建一個Todo其中:

session.Todo.create(props) 

拋出error:提供給Todo.create

無效道具tags[0]

當與props叫:

{ 
"text":"Test my data", 
"tags":[ 
    "urgent", 
    "personal" 
    ], 
    "user":0 
} 

的驗證似乎與創建模型來干擾。當創建標籤,指定PropTypes是:

const { string, number, arrayOf oneOfType, instanceOf } = PropTypes 
Todo.propTypes = { 
    text: string, 
    user: oneOfType([ 
     number, 
     instanceOf(User) 
    ]), 
    tags: oneOfType([ 
     arrayOf(string), 
     arrayOf(
     instanceOf(Tag) 
    ) 
    ]) 
} 

在終極版-ORM白手創業或創建模型時,你可以提供的ID或模型的相關屬性的實例。因此,確保兩者都被Model接受是非常祕密的。

0

至於以後,我可以從你的代碼似乎看到像你已經錯過的代碼在您的模型中定義減速器(TODO, TAG, USER)

根據文檔here

你需要在每個模型中有一個靜態方法。

static reducer(state, action, Tag) static reducer(state, action, TODO) static reducer(state, action, USER)

+0

我將模型添加到問題中,您可以看到我已經爲每個模型定義了一個縮減器。 – vamsiampolu

相關問題