Comments (5)
// Example of my converted payload
{
"data": [
{
"type": "party.address",
"id": 36,
"attributes": {
"id": 36
},
"relationships": {
"subdivision": null
}
}
],
"included": []
}
this is still not valid JSON:API, I'll fix it for you maybe that's the entire issue here
// Example of my converted payload
{
"data": [
{
"type": "party.address",
"id": 36,
"attributes": {
"id": 36
},
"relationships": {
"subdivision": { data: null } // this is the key change
}
}
],
"included": []
}
from data.
that's the entire issue here
"subdivision": { data: null } // this is the key change
...... don't have to say anything anymore ..... That was the culprit.
from data.
Feels like a hack but when I convert the payload to JSON-API I get something like:
data: [{
id: 45,
type: 'party.address',
attributes: { },
relationships: {
country: {
id: 1,
type: 'country',
attributes: { }
},
subdivision: {
id: null,
type: 'country.subdivision'
}
}
}]
So when the relationships are handled in:
data/packages/json-api/src/-private/cache.ts
Lines 1289 to 1315 in b9685e8
I added a small piece of code which checks if the id is null
and if so, it adds a replaceRelatedRecord
operation.
function setupRelationships(
storeWrapper: CacheStoreWrapper,
identifier: StableRecordIdentifier,
data: JsonApiResource
) {
// TODO @runspired iterating by definitions instead of by payload keys
// allows relationship payloads to be ignored silently if no relationship
// definition exists. Ensure there's a test for this and then consider
// moving this to an assertion. This check should possibly live in the graph.
const relationships = storeWrapper.getSchemaDefinitionService().relationshipsDefinitionFor(identifier);
const keys = Object.keys(relationships);
for (let i = 0; i < keys.length; i++) {
const relationshipName = keys[i];
const relationshipData = data.relationships![relationshipName];
if (!relationshipData) {
continue;
}
if (relationshipData.data.id === null) {
graphFor(storeWrapper).push({
op: 'replaceRelatedRecord',
record: identifier,
field: relationshipName,
value: null
});
continue;
}
graphFor(storeWrapper).push({
op: 'updateRelationship',
record: identifier,
field: relationshipName,
value: relationshipData,
});
}
}
I know for sure you will come up with a much better and much more robust idea, but this works for me very reliably.
from data.
data: [{
id: 45,
type: 'party.address',
attributes: { },
relationships: {
country: {
id: 1,
type: 'country',
attributes: { }
},
subdivision: {
id: null,
type: 'country.subdivision'
}
}
}]
note this isn't the right JSON:API format and that's probably why this doesn't just work. The value for subdivision
should just be null
There's a chance this is occurring because of using the wrong graph operation. If so I'd need more information, and would be happy to pair. I'd really like to get the graph package firmed up and made public soon
from data.
The value for
subdivision
should just be null
When I do that, the data is ignored because of
data/packages/json-api/src/-private/cache.ts
Line 1304 in 0c11db2
so no notification that the record is updated. The
subdivision
should still be in the relations
object right?
... I'd need more information ...
What I really do is converting my JSONRPC payload into a JSONAPI payload and then I call super
to let @ember-data
do the rest. I pick it up again when I instantiate
the record or getting the change notification.
// extending the JSON-API cache
class RPCCache extends Cache {
// convert the JSON response into a JSONAPI response
convertRPC(payload, model) {
.....
const result = {
data: res,
included: includes
}
return result;
}
put(document) {
const method = document.request.data.method.split('.');
const model = method.slice(1, method.length - 1).join('.');
let content = document.content
// convert the JSON-RPC payload from the backend to the JSON-API payload
const payload = this.convertRPC(content, model);
document.content = payload;
return super.put(...arguments);
}
}
// Example of my converted payload
{
"data": [
{
"type": "party.address",
"id": 36,
"attributes": {
"id": 36
},
"relationships": {
"subdivision": null
}
}
],
"included": []
}
There are no manual changes. The usecase I described is called an on_change
. So when a field changes it will notify the backend that it has changed. Then the backend returns an object with data as response. That data is then fed into @ember-data
which then notifies the change and my record magically updates the fields which were in the response. This works perfectly for all usecases. Sometimes 6 fields has to be updated which happens without any problem, but when a field should be emptied (backend returns a null
for that field) that fails.
So in short:
- field is empty at the start
- field is filled based on another field which filtered out the records
- field must be emptied because that other field has changed which notified the backend who returned that the data of this field should be
null
. However this is never pushed into the cache and no notification.
from data.
Related Issues (20)
- [Question] Get an already fetched record from cache HOT 4
- [Store] Getting relationship data HOT 5
- [SchemaDefinitionService] Graph expects a 'Model' Object layout HOT 2
- Error: can't locate @ember-data/record-data/-private referred to in module rules HOT 2
- Mutated data using setAttr or saving the record gives an error HOT 8
- 4.12.x - this.requestManager is undefined HOT 8
- [email protected] broke `findAll`
- LegacyHandler in 4.12 providing frozen options to Adapters
- 4.12 LegacyRelationshipSupport caches first accessed async relationship instead of by-field HOT 4
- deleteRecord/unloadRecord causing peekAll to return undefined records HOT 1
- Installing ember-data independently: build fails HOT 2
- Broken links in API class documentation HOT 1
- Typescript doesn't match runtime HOT 2
- helpful errors out of date HOT 2
- Build fails on `4.12.0` if project folder name contains `ember-data-` HOT 10
- Conflict with ember-simple-auth: On 4.12.0, ember-data appears to lead to ember-simple-auth sessions to be invalidated right after login HOT 16
- destroyRecord removes two items from collection HOT 3
- Deleting a record causes Record Array to dissapear HOT 2
- Model attrs not accessible after destroying HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from data.