Skip to content

loadRelations loop #135

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

Closed
Patrik-Lundqvist opened this issue Aug 24, 2014 · 8 comments
Closed

loadRelations loop #135

Patrik-Lundqvist opened this issue Aug 24, 2014 · 8 comments
Assignees
Milestone

Comments

@Patrik-Lundqvist
Copy link

Loading a second level of nested relations seems to create an infinite loop

Example:

// Load first customer
var customer1 = DS.get('customer', 1);

// Load the customers projects
DS.loadRelations('customer', customer1, ['project']).then(function () {
    var project1 = DS.get('project', 1);

    // Load the projects items
    DS.loadRelations('project', project1, ['item']);
});

Loading the projects items creates a loop which freezes the browser

@jmdobry
Copy link
Member

jmdobry commented Aug 24, 2014

@Patrik-Lundqvist Can I see how you've defined the relations for these resources?

@Patrik-Lundqvist
Copy link
Author

Yes of course

DS.defineResource({
    name: 'customer',
    endpoint: 'customers',
    relations: {
        belongsTo: {
            organization: {
                parent: true,
                localKey: 'organizationId',
                localField: 'organization'
            }
        },
        hasMany: {
            project: {
                localField: 'projects',
                foreignKey: 'customerId'
            }
        }
    }
});

DS.defineResource({
    name: 'project',
    endpoint: 'projects',
    relations: {
        belongsTo: {
            customer: {
                parent: true,
                localKey: 'customerId',
                localField: 'customer'
            }
        },
        hasMany: {
            item: {
                localField: 'items',
                foreignKey: 'projectId'
            }
        }
    }
});

DS.defineResource({
    name: 'item',
    endpoint: 'items',
    relations: {
        belongsTo: {
            project: {
                parent: true,
                localKey: 'projectId',
                localField: 'project'
            }
        }
    }
});

Here is the logged error: http://pastebin.com/ntbCMbdn

jmdobry added a commit that referenced this issue Aug 24, 2014
@jmdobry
Copy link
Member

jmdobry commented Aug 24, 2014

@Patrik-Lundqvist The test I added in the above commit seems to reflect the relations you described, and it passes. At this point I would need you to create a Plunkr or JSBin which I can debug.

@jmdobry jmdobry self-assigned this Aug 24, 2014
@Patrik-Lundqvist
Copy link
Author

The problem was in my data from the api. The returned item json object had a nested foreign key relation which caused this problem.

Example:

// This data will cause the problem
{
    "id": 1,
    "name": "Item1",
    "projectId": 1,
    "project": {
      "id": 1
    }
}

// This will not
{
    "id": 1,
    "name": "Item1",
    "projectId": 1
}

Handling of nested foreign key relations in returned data should probably be its own issue.

@jmdobry
Copy link
Member

jmdobry commented Aug 25, 2014

It should have worked with the nested data. I'll investigate that.

@jmdobry
Copy link
Member

jmdobry commented Aug 25, 2014

@Patrik-Lundqvist Actually, can you post here what endpoints were hit (the urls) and what data was returned by each?

@Patrik-Lundqvist
Copy link
Author

Here is a plunker displaying the error: http://plnkr.co/edit/U7j42d?p=preview

@jmdobry
Copy link
Member

jmdobry commented Aug 25, 2014

Thanks

@jmdobry jmdobry added this to the 1.0.0-beta.2 milestone Aug 25, 2014
jmdobry added a commit that referenced this issue Aug 25, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants