Skip to content

Commit 7a06fc1

Browse files
committed
Fixes for Issue #5
1 parent b811e23 commit 7a06fc1

File tree

1 file changed

+48
-62
lines changed

1 file changed

+48
-62
lines changed

src/ParseModel.js

Lines changed: 48 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ class ParseModel extends Model
112112

113113
const classNameOrParseObject = options.parseObject || options.className;
114114

115+
Debug.log(`ParseModel - ctor - 0 - options.parseObject: ${options.parseObject}`, true);
116+
Debug.log(`ParseModel - ctor - 0 - options.className: ${options.className}`);
117+
115118
if (classNameOrParseObject instanceof Parse.Object)
116119
{
117120
const parseObject = classNameOrParseObject;
@@ -128,6 +131,8 @@ class ParseModel extends Model
128131
* @type {BackboneParseObject}
129132
*/
130133
this.parseObject = new BackboneParseObject(parseObject.className, parseObject.attributes);
134+
this.parseObject.id = parseObject.id;
135+
this.parseObject._localId = parseObject._localId;
131136

132137
adjustedClassName = this.parseObject.className;
133138
}
@@ -308,24 +313,6 @@ class ParseModel extends Model
308313
return xhr;
309314
}
310315

311-
/**
312-
* In order to deserialize Classes associated with `Parse.Object.registerSubclass()` this method is called and
313-
* the data must be forwarded onto the backing Parse.Object of ParseModel.
314-
*
315-
* @param {object} serverData - server data attributes.
316-
* @private
317-
*/
318-
_finishFetch(serverData)
319-
{
320-
this.parseObject._finishFetch(serverData);
321-
322-
const options = { updateParseObject: false };
323-
324-
const attrs = this.parse(this.parseObject, options) || {};
325-
326-
this.set(attrs, options);
327-
}
328-
329316
/**
330317
* Has this model been saved to the server yet? If the model does not yet have an id, it is considered to be new.
331318
*
@@ -355,9 +342,8 @@ class ParseModel extends Model
355342
{
356343
/* eslint-enable no-unused-vars */
357344

358-
Debug.log(`ParseModel - parse - 0 - resp instanceof Parse.Object: ${resp instanceof Parse.Object}`, true);
359-
360-
Debug.log(`ParseModel - parse - 1 - ParseModel.prototype.idAttribute: ${ParseModel.prototype.idAttribute}`);
345+
Debug.log(`ParseModel - parse - 0 - resp instanceof Parse.Object: ${resp instanceof Parse.Object}`, true);
346+
Debug.log(`ParseModel - parse - 1 - ParseModel.prototype.idAttribute: ${ParseModel.prototype.idAttribute}`);
361347

362348
let merged;
363349

@@ -373,21 +359,21 @@ class ParseModel extends Model
373359
const mergeId = {};
374360
mergeId[ParseModel.prototype.idAttribute] = this.id;
375361

376-
Debug.log(`ParseModel - parse - 2 - mergeId: ${mergeId[ParseModel.prototype.idAttribute]}`);
362+
Debug.log(`ParseModel - parse - 2 - mergeId: ${mergeId[ParseModel.prototype.idAttribute]}`);
377363

378364
merged = _.extend(mergeId, resp.attributes);
379365

380-
Debug.log(`ParseModel - parse - 3 - merged: ${JSON.stringify(merged)}`);
366+
Debug.log(`ParseModel - parse - 3 - merged: ${JSON.stringify(merged)}`);
381367
}
382368
else if (_.isObject(resp))
383369
{
384370
const parseObjectId = resp[ParseModel.prototype.idAttribute];
385371

386-
Debug.log(`ParseModel - parse - 4 - resp is an Object / existing model - parseObjectId: ${parseObjectId}; resp: ${JSON.stringify(resp)}`);
372+
Debug.log(`ParseModel - parse - 4 - resp is an Object / existing model - parseObjectId: ${parseObjectId}; resp: ${JSON.stringify(resp)}`);
387373

388374
if (!_.isUndefined(parseObjectId) && this.id !== parseObjectId)
389375
{
390-
Debug.log(`ParseModel - parse - 5 - this.id !== parseObjectId; this.id: ${this.id}; parseObjectId: ${parseObjectId}`);
376+
Debug.log(`ParseModel - parse - 5 - this.id !== parseObjectId; this.id: ${this.id}; parseObjectId: ${parseObjectId}`);
391377

392378
this.id = parseObjectId;
393379
}
@@ -453,38 +439,50 @@ class ParseModel extends Model
453439

454440
if (Utils.isNullOrUndef(key) || typeof key === 'object')
455441
{
442+
Debug.log(`ParseModel - save - 0`);
443+
456444
attrs = key;
457445
options = val;
458446
}
459447
else
460448
{
449+
Debug.log(`ParseModel - save - 1`);
450+
461451
(attrs = {})[key] = val;
462452
}
463453

464454
// Save any previous options.success function.
465455
const success = !Utils.isNullOrUndef(options) ? options.success : undefined;
466456

467-
options = _.extend(
457+
Debug.log(`ParseModel - save - 2 - options.success defined: ${success !== undefined}`);
458+
459+
options = options || {};
460+
461+
options.success = (model, resp, options) =>
468462
{
469-
success: (model, resp, options) =>
463+
// Execute previously cached success function. Must do this first before resolving any potential
464+
// child object changes.
465+
if (success)
470466
{
471-
const modelAttrs = this.attributes;
472-
for (const attr in modelAttrs)
467+
Debug.log('ParseModel - save - 3 - invoking original options.success.');
468+
success.call(options.context, this, resp, options);
469+
}
470+
471+
Debug.log('ParseModel - save - 4 - invoking ParseModel success.');
472+
473+
const modelAttrs = this.attributes;
474+
for (const attr in modelAttrs)
475+
{
476+
const field = modelAttrs[attr];
477+
478+
// Here is the key part as if the associated Parse.Object id is different than the model id it
479+
// needs to be parsed and data set to the Backbone.Model.
480+
if (field.parseObject && field.parseObject.id !== field.id)
473481
{
474-
const field = modelAttrs[attr];
475-
476-
// Here is the key part as if the associated Parse.Object id is different than the model id it
477-
// needs to be parsed and data set to the Backbone.Model.
478-
if (field.parseObject && field.parseObject.id !== field.id)
479-
{
480-
field.set(field.parse(field.parseObject), options);
481-
}
482+
field.set(field.parse(field.parseObject), options);
482483
}
483-
484-
// Execute previously cached success function.
485-
if (success) { success.call(options.context, this, resp, options); }
486484
}
487-
}, options);
485+
};
488486

489487
return super.save(attrs, options);
490488
}
@@ -536,7 +534,7 @@ class ParseModel extends Model
536534
const changing = this._changing;
537535
this._changing = true;
538536

539-
Debug.log(`ParseModel - set - 0 - changing: ${changing}; attrs: ${JSON.stringify(attrs)}; options: ${JSON.stringify(options)}`, true);
537+
Debug.log(`ParseModel - set - 0 - changing: ${changing}; attrs: ${JSON.stringify(attrs)}; options: ${JSON.stringify(options)}`, true);
540538

541539
if (!changing)
542540
{
@@ -555,22 +553,22 @@ class ParseModel extends Model
555553

556554
if (!_.isEqual(current[attr], val))
557555
{
558-
Debug.log(`ParseModel - set - 1 - current[attr] != val for key: ${attr}`);
556+
Debug.log(`ParseModel - set - 1 - current[attr] != val for key: ${attr}`);
559557
changes.push(attr);
560558
}
561559

562560
let actuallyChanged = false;
563561

564562
if (!_.isEqual(prev[attr], val))
565563
{
566-
Debug.log(`ParseModel - set - 2 - prev[attr] != val for key: ${attr}`);
564+
Debug.log(`ParseModel - set - 2 - prev[attr] != val for key: ${attr}`);
567565

568566
changed[attr] = val;
569567
actuallyChanged = true;
570568
}
571569
else
572570
{
573-
Debug.log(`ParseModel - set - 3 - prev[attr] == val delete changed for key: ${attr}`);
571+
Debug.log(`ParseModel - set - 3 - prev[attr] == val delete changed for key: ${attr}`);
574572
delete changed[attr];
575573
}
576574

@@ -589,7 +587,7 @@ class ParseModel extends Model
589587
// Parse.Object returns itself on success
590588
unsetSuccess = this.parseObject === this.parseObject.unset(attr);
591589

592-
Debug.log(`ParseModel - set - 4 - unset Parse.Object - attr: ${attr}; unsetSuccess: ${unsetSuccess}`);
590+
Debug.log(`ParseModel - set - 4 - unset Parse.Object - attr: ${attr}; unsetSuccess: ${unsetSuccess}`);
593591
}
594592

595593
if (unsetSuccess)
@@ -599,15 +597,15 @@ class ParseModel extends Model
599597
}
600598
else
601599
{
602-
let setSuccess = !updateParseObject;
600+
let setSuccess = !updateParseObject || attr === ParseModel.prototype.idAttribute;
603601

604602
if (actuallyChanged && updateParseObject && this.parseObject !== null &&
605603
attr !== ParseModel.prototype.idAttribute)
606604
{
607605
// Parse.Object returns itself on success
608606
setSuccess = this.parseObject === this.parseObject.set(attr, val, options);
609607

610-
Debug.log(`ParseModel - set - 5 - set Parse.Object - attr: ${attr}; setSuccess: ${setSuccess}`);
608+
Debug.log(`ParseModel - set - 5 - set Parse.Object - attr: ${attr}; setSuccess: ${setSuccess}`);
611609
}
612610

613611
if (actuallyChanged && setSuccess)
@@ -624,7 +622,7 @@ class ParseModel extends Model
624622
for (let i = 0; i < changes.length; i++)
625623
{
626624
this.trigger(`change:${changes[i]}`, this, current[changes[i]], options);
627-
Debug.log(`ParseModel - set - 6 - trigger - changeKey: ${changes[i]}`);
625+
Debug.log(`ParseModel - set - 6 - trigger - changeKey: ${changes[i]}`);
628626
}
629627
}
630628

@@ -638,26 +636,14 @@ class ParseModel extends Model
638636
options = this._pending;
639637
this._pending = false;
640638
this.trigger('change', this, options);
641-
Debug.log(`ParseModel - set - 7 - trigger - change`);
639+
Debug.log(`ParseModel - set - 7 - trigger - change`);
642640
}
643641
}
644642
this._pending = false;
645643
this._changing = false;
646644
return this;
647645
}
648646

649-
/**
650-
* In order to deserialize Classes associated with `Parse.Object.registerSubclass()` this method is called and
651-
* the data must be forwarded onto the backing Parse.Object of ParseModel.
652-
*
653-
* @param {boolean} existed - Sets the existed state.
654-
* @private
655-
*/
656-
_setExisted(existed)
657-
{
658-
this.parseObject._setExisted(existed);
659-
}
660-
661647
/**
662648
* Return a copy of the model's `attributes` object.
663649
*

0 commit comments

Comments
 (0)