-
Notifications
You must be signed in to change notification settings - Fork 31
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
Save with wait:true
, patch: true
and custom serialize()
method
#64
base: master
Are you sure you want to change the base?
Changes from all commits
b94e6c9
c421529
71c0a5c
78d6098
6e885a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,10 @@ var assign = require('lodash.assign'); | |
var isObject = require('lodash.isobject'); | ||
var clone = require('lodash.clone'); | ||
var result = require('lodash.result'); | ||
var transform = require('lodash.transform'); | ||
var keys = require('lodash.keys'); | ||
var intersection = require('lodash.intersection'); | ||
var reduce = require('lodash.reduce'); | ||
|
||
// Throw an error when a URL is needed, and none is supplied. | ||
var urlError = function () { | ||
|
@@ -20,6 +24,31 @@ var wrapError = function (model, options) { | |
}; | ||
}; | ||
|
||
// recursive function to find the attrs in the serialized object | ||
var transformForPatch = function transformForPatch(obj, attrs) { | ||
|
||
if (!isObject(obj)) { | ||
return obj; | ||
} | ||
|
||
var attrKeys = keys(attrs); | ||
|
||
// If all attributes are within the object | ||
if (intersection(keys(obj), attrKeys).length === attrKeys.length) { | ||
// return the subset of passed-in attrs | ||
return reduce(attrKeys, function(redux, attrKey) { | ||
redux[attrKey] = attrs[attrKey]; | ||
return redux; | ||
}, {}); | ||
} | ||
|
||
return transform(obj, function(result, val, key) { | ||
// run transformForPatch on the next level | ||
result[key] = transformForPatch(val, attrs); | ||
}); | ||
}; | ||
|
||
|
||
var Model = State.extend({ | ||
save: function (key, val, options) { | ||
var attrs, method; | ||
|
@@ -60,10 +89,18 @@ var Model = State.extend({ | |
wrapError(this, options); | ||
|
||
method = this.isNew() ? 'create' : (options.patch ? 'patch' : 'update'); | ||
if (method === 'patch') options.attrs = attrs; | ||
|
||
if (method === 'patch') { | ||
options.attrs = transformForPatch(this.serialize(), attrs); | ||
} | ||
// if we're waiting we haven't actually set our attributes yet so | ||
// we need to do make sure we send right data | ||
if (options.wait && method !== 'patch') options.attrs = assign(model.serialize(), attrs); | ||
if (options.wait && method !== 'patch') { | ||
var clonedModel = new this.constructor(this.getAttributes({props: true})); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this the one that was causing errors when using |
||
clonedModel.set(attrs); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see, this is so you can call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol, correct. The issue with |
||
|
||
options.attrs = clonedModel.serialize(); | ||
} | ||
var sync = this.sync(method, this, options); | ||
|
||
// Make the request available on the options object so it can be accessed | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super nitpicky performance tweak would be to move this before the
keys(attrs)
line above.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing.