-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updating related fields from memoized selector #218
Comments
So I ended up with having a parent listen for the creayion of B,C,D and update itself with timestamp. class A extends Model {
static reducer(action, A, session) {
switch (action.type) {
case CREATE_A: {
A.create({ action.payload });
break;
}
case CREATE_B:
case CREATE_C:
case CREATE_D:
const id = action.payload.a_id; // The id of a is in the payload for B,C,D
A.withId(id).update({"touched": new Date()})
default:
}
}
} |
If I understand your use-case correctly, this should not be necessary at all. What are your |
Thanks. class Project extends Model {
toString() {
return `Project: ${this.id}`;
}
static reducer(action, Project, session) {
switch (action.type) {
case RECEIVE_PROJECT: {
const { id, name } = action.payload;
Project.create({ id, name });
break;
}
default:
break;
}
}
}
Project.modelName = 'Project';
Project.fields = {
id: attr(),
name: attr(),
touched: attr()
}; The dataset class Dataset extends Model {
toString() {
return `Dataset: ${this.id}`;
}
static reducer(action, Dataset, session) {
switch (action.type) {
case CREATE_DATASET: {
Dataset.upsert(action.payload);
break;
}
default:
return undefined;
}
}
}
Dataset.modelName = 'Dataset';
Dataset.fields = {
id: attr(),
name: attr(),
id_field: attr(),
aggregation_field: attr(),
order_field: attr(),
content_field: attr(),
project: fk('Project', 'datasets')
}; The project selector const _projectSelector = createSelector(
orm,
state => state.orm,
session => {
return session.Project.all().toModelArray();
}
); And then, in a connected component that uses that selector (and takes the first element in the array) I have {props.project.datasets
.toModelArray()
.map(dataset => (
<ListGroupItem key={dataset.id}>{dataset.name}</ListGroupItem>
))}
or more simply this Neither of which updates when I add a new dataset. I've confirmed the dataset is updated with the proper id of the project, and the project exists in the orm with that id. Thanks |
Using models and relationship accessors outside of selectors is not supported by design. In this case specifically you are first executing the selector and accessing Please use const _projectDatasets = createSelector(
orm,
state => state.orm,
session => session.Project.toModelArray().reduce((map, project) => ({
...map,
[project.id]: project.datasets.toRefArray(),
}), {})
); And then in your components, {props.projectDatasets[props.project.id]
.map(dataset => (
<ListGroupItem key={dataset.id}>{dataset.name}</ListGroupItem>
))} |
Oh that's elegant. Thanks. |
Closing in favor of #253. |
Hi, I wonder if we can reopen this issue. const getRoomID = (state, props) => props.roomID;
const getRoom = createSelector(orm, dbSelector, getRoomID, (session, roomID) => {
const room = session.Rooms.withId(roomID);
return room;
});
const getRoomMessages = createSelector(orm, dbSelector, getRoom, (session, room) => room.messages.all().toRefArray() ); But the problem is that either room or data may not exist and we want our app to be fault-tolerant. So we should return a default case when either of them doesn't exist. const getRoom = createSelector(..., (session, roomID) => {
return session.Rooms.withId(roomID) || {};
});
const getRoomMessages = createSelector(..., (session, room) => room.messages ? room.messages.all().toRefArray() : [] ); But now if the room doesn't exist the first time we use the |
@Eddie-CooRo The problem here, again, is returning models from selectors. Because this happens so often I'm starting to think that we should actually forbid returning model instances by throwing an error. Selectors were not composable the way you did it. You would have had to call const getRoom = createSelector(orm.Rooms);
const getRoomMessages = createSelector(orm.Rooms.messages); |
Hi,
We're super happy to see this project moving again! We're still a few versions back, and ran into an issue.
Anyway, we have model A and model B. B has a foreign key to A.
At some point we call an instance of A and dispatch an action to add new B's to it.
We're getting A via a memoized selector, and it looks like it doesn't get an indication that a B was added, so A.bs.toModelArray doesn't update with the B that was added.
I couldn't tell if this is a known issue, by design or something resolved recently.
We'd like to know if the newer versions address this or is their something we can do
Thanks
The text was updated successfully, but these errors were encountered: