Comments (8)
I can verify this. If the structure of the model is fundamentally changed in a custom serialize
method, then the simple lodash.assign
if {wait: true}
doesn't take that into account.
The only way I can imagine this working in the current form is to create a dummy model with the new attributes, serialize that (using the same serialize
method), and merge the objects for the sync
request.
Example time. If this is my serialize method:
serialize() {
const res = this.getAttributes({props: true}, true);
const id = res.id;
forEach(this._children, function(value, key) {
res[key] = this[key].serialize();
}, this);
forEach(this._collections, function(value, key) {
res[key] = this[key].serialize();
}, this);
delete res.id;
return {
id,
attributes: res,
};
}
It turns returns an object that looks like this:
{
"id":"11",
"attributes": {
"date_end":null,
"date_start":1344744000000
}
}
If I do myModel.save({date_end: 1439586948920}, {wait: true});
, the save attributes are added to the outer object, because of the simple lodash.assign
{
"id":"11",
"attributes": {
"date_end":null,
"date_start":1344744000000
},
"date_end":1439586948920
}
from ampersand-model.
I believe this would be solved by #58
Re-open this if it didn't.
from ampersand-model.
@pgilad I cannot reopen, but this issue was not resolved.
The problem is that this line:
if (options.wait && method !== 'patch') options.attrs = assign(model.serialize(), attrs);
assigns attrs
AFTER the model has been serialized. This assumes that the server expects the attributes to exist at the top level. Not all servers do.
In the wait:true
situation, options.attrs
should be set to a cloned copy of the model which has its attributes changed, and is THEN serialized. This avoids changing the original model until sync, while allowing the modified model to be serialized as the user directs.
I will make a PR if I get to resolving this in my project, but it's currently a lower priority.
from ampersand-model.
Actually started working on this, and the complexity to support all of these simultaneously quickly becomes staggering:
wait: true
patch: true
- custom
serialize
that alters the form of the data
You can do 2 at a time fairly easily, but to do all 3 requires keeping track of the changed attributes (already done by state), and also being aware of way the model is serialized in order to cherry-pick those attributes out of the data.
Not impossible, but possibly ill-advised.
Which brings me to my next point: don't devise APIs that require data attributes to be in a nested object.
from ampersand-model.
Circling back on this so the record is here. I ended up extending AmpersandModel to support JSON-API and just got around to releasing it. I changed save
within that extension to respect wait:true
, patch:true
, and a custom serialize
method.
https://github.com/bobholt/ampersand-jsonapi-model
I think the strategy there should work within AmpersandModel. I should hopefully have some time next week to make a PR here that accomplishes the same thing.
from ampersand-model.
Thanks for keeping with this @bobholt, sorry it didn't get reopened. Doing that now.
from ampersand-model.
Thanks @Wrathgar.
from ampersand-model.
Submitted fix for this as #64.
from ampersand-model.
Related Issues (20)
- Automatically setting collection and collection items parent for models initialized from JSON HOT 3
- success handlers dont get access to the actual http response
- Relative urlRoot
- wrapError stripping ampersand-sync's err.message. HOT 3
- Save options wait:true and patch:true are mutually exclusive HOT 4
- Use ampersand-sync 3.0.7 HOT 1
- Evaluate changes in ampersand-sync 4.0
- Adopt ampersand-sync v4.0
- A collection declared as a prop introduces errors. HOT 2
- Props array type doesn't update until after .save() HOT 3
- model.destroy() returning opts.success() with no DELETE xhr created HOT 3
- When using ampersand-model I cannot save a prop called `url` HOT 3
- Mixing in data for every request HOT 1
- local data HOT 4
- update ampersand-sync to 4.0.3
- Error when using latest ampersand-state HOT 2
- Incompatibility with Webpack?
- Missing attributes from server are not removed HOT 3
- Default content type to text/plain;charset=UTF-8 with ajaxConfig HOT 2
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 ampersand-model.