Created
September 21, 2018 17:09
-
-
Save syntruth/a76762aab0e442833bfd88bd937aa766 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1091a1092,1093 | |
> // This will be memoized after the first call and will default | |
> // to 'id'. | |
1093c1095,1131 | |
< return attrs[this.model.prototype.idAttribute || 'id']; | |
--- | |
> if (this._modelId) return attrs[this._modelId]; | |
> | |
> this._modelId = this._findIdAttribute(attrs) | |
> | |
> return attrs[this._modelId]; | |
> }, | |
> | |
> _findIdAttribute: function(attrs) { | |
> var id = 'id'; | |
> | |
> var modelType = typeof this.model; | |
> | |
> if (modelType == 'function') { | |
> // We call the method to obtain the model and then | |
> // examine it to try and grab the idAttribute value. | |
> var result = this.model(attrs) | |
> | |
> // If the result is a Model, we can examine its | |
> // idAttribute property directly. | |
> if (result instanceof Model) { id = result.idAttribute } | |
> | |
> // Otherwise see if it has an idAttribute | |
> else { | |
> var idAttr = typeof result.idAttribute; | |
> | |
> if (idAttr == 'function') { | |
> // Is a static method | |
> id = result.idAttribute(); | |
> } | |
> else if (idAttr == 'string') { | |
> // Is a class property | |
> id = result.idAttribute; | |
> } | |
> } | |
> } | |
> | |
> return id; | |
1113c1151,1155 | |
< var model = new this.model(attrs, options); | |
--- | |
> | |
> var model = this._getModel(attrs, options); | |
> | |
> if (!model) return false; | |
> | |
1118a1161,1188 | |
> _getModel(attrs, options) { | |
> var result = false; | |
> | |
> try { | |
> result = new this.model(attrs, options); | |
> | |
> return result; | |
> } | |
> catch (err) { | |
> if (err.message.indexOf('is not a constructor') >= 0) { | |
> // If we didn't have a constructor, then check for a | |
> // return result that might be a Model or a Model | |
> // constructor itself. | |
> if (typeof(this.model) == 'function') { | |
> result = this.model(attrs, options) | |
> | |
> if (result instanceof Model) return result; | |
> | |
> if (typeof(result) == 'function') { | |
> result = new result(attrs, options); | |
> } | |
> } | |
> } | |
> } | |
> | |
> return result; | |
> }, | |
> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment