Skip to content
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

nested relationships part 2 #14

Closed
PaulusMenheere opened this issue Sep 19, 2016 · 1 comment
Closed

nested relationships part 2 #14

PaulusMenheere opened this issue Sep 19, 2016 · 1 comment

Comments

@PaulusMenheere
Copy link

I've made some adjustments to json-api.model.js to make it more robust..

"use strict";
var _ = require('underscore');
require('reflect-metadata');
var JsonApiModel = (function () {
    function JsonApiModel(data) {
        if (data) {
            this.id = data.id;
            _.extend(this, data.attributes);
        }
    }
    JsonApiModel.prototype.syncRelationships = function (data, included, level, datastore) {
        if (data) {
            this.parseHasMany(data, included, level, datastore);
            this.parseBelongsTo(data, included, level, datastore);
        }
    };
    JsonApiModel.prototype.parseHasMany = function (data, included, level, datastore) {
        var hasMany = Reflect.getMetadata('HasMany', this);
        if (hasMany) {
            for (var _i = 0, hasMany_1 = hasMany; _i < hasMany_1.length; _i++) {
                var metadata = hasMany_1[_i];
                if (data.relationships[metadata.relationship] && data.relationships[metadata.relationship].data && data.relationships[metadata.relationship].data.length > 0) {
                    var typeName = data.relationships[metadata.relationship].data[0].type;
                    var objectType = Reflect.getMetadata('JsonApiDatastoreConfig', datastore.constructor).models[typeName];
                    this[metadata.propertyName] = this.getHasManyRelationship(objectType, data, included, metadata.relationship, typeName, level, datastore);
                }
            }
        }
    };
    JsonApiModel.prototype.parseBelongsTo = function (data, included, level, datastore) {
        var belongsTo = Reflect.getMetadata('BelongsTo', this);
        if (belongsTo) {
            for (var _i = 0, belongsTo_1 = belongsTo; _i < belongsTo_1.length; _i++) {
                var metadata = belongsTo_1[_i];
                if (data.relationships[metadata.relationship] && data.relationships[metadata.relationship].data) {
                    var typeName = data.relationships[metadata.relationship].data.type;
                    var objectType = Reflect.getMetadata('JsonApiDatastoreConfig', datastore.constructor).models[typeName];
                    this[metadata.propertyName] = this.getBelongsToRelationship(objectType, data, included, metadata.relationship, typeName, level, datastore);
                }
            }
        }
    };
    JsonApiModel.prototype.getHasManyRelationship = function (objectType, data, included, relationship, typeName, level, datastore) {
        var relationshipList = [];
        data.relationships[relationship].data.forEach(function (item) {
            var relationshipData = _.findWhere(included, { id: item.id, type: typeName });
          if( relationshipData ) {
            var newObject = new objectType(relationshipData);
            if (relationshipData.relationships && level <= 1) {
              newObject.syncRelationships(relationshipData, included, ++level, datastore);
            }
            relationshipList.push(newObject);
          }
        });
        return relationshipList;
    };
    JsonApiModel.prototype.getBelongsToRelationship = function (objectType, data, included, relationship, typeName, level, datastore) {
        var id = data.relationships[relationship].data.id;
        var relationshipData = _.findWhere(included, { id: id, type: typeName });
        if(relationshipData) {
          var newObject = new objectType(relationshipData);
          if (relationshipData.relationships && level <= 1) {
            newObject.syncRelationships(relationshipData, included, ++level, datastore);
          }
          return newObject;
        }
    };
    return JsonApiModel;
}());
exports.JsonApiModel = JsonApiModel;
//# sourceMappingURL=json-api.model.js.map
@ghidoz
Copy link
Owner

ghidoz commented Sep 22, 2016

Hi, thanks. I released the v3.1.1, implementing some of your suggestions.
I don't think that relationshipData.relationships is useful, since it's always present, even if there isn't anything included.

@ghidoz ghidoz closed this as completed Sep 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants