Skip to content

Linked Resources #237

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
AlJohri opened this issue Nov 10, 2014 · 3 comments
Closed

Linked Resources #237

AlJohri opened this issue Nov 10, 2014 · 3 comments

Comments

@AlJohri
Copy link

AlJohri commented Nov 10, 2014

I saw this issue regarding nested resources: #40

I'm trying to do something similar but just link resources instead.

If a single "listing" object looks like:

URL: http://nucraigslist.herokuapp.com/api/v1/listing/798128320234094/

{
    "approved": false,
    "buy_or_sell": "sell",
    "category": "tickets",
    "created_time": "2014-11-04T23:58:36",
    "id": "798128320234094",
    "message": "Selling two Capital Cities tickets for their show this Saturday at The Rave in Milwaukee. $15 each OBO.",
    "parsed": true,
    "resource_uri": "/api/v1/listing/798128320234094/",
    "seller": "/api/v1/seller/10203068149729605/",
    "sold": false,
    "type": "",
    "updated_time": "2014-11-05T01:25:36"
}

EDIT: just wanted to add that I can easily change "seller": "/api/v1/seller/10203068149729605/" into a "seller_id": 10203068149729605

URL: http://nucraigslist.herokuapp.com/api/v1/seller/10203068149729605/

{
    "id": "10203068149729605",
    "name": "Zach Silva",
    "resource_uri": "/api/v1/seller/10203068149729605/"
}

and I have two resources set up:

app.factory('Listing', ['DS', function (DS) {
  return DS.defineResource({ 
    name: 'listing', 
    baseUrl: '/api/v1',
    deserialize: function(name, data) { 
        return data.data.objects;
    }
  });
}]);

app.factory('Seller', ['DS', function (DS) {
  return DS.defineResource({ 
    name: 'seller', 
    baseUrl: 'api/v1',
    deserialize: function(name, data) { 
      return data.data.objects;
    }
  });
}]);

how can I link the listing and seller objects so if have the following code:

Listing.findAll();
Seller.findAll();
Listing.bindAll($scope, 'listings', {});
Seller.bindAll($scope, 'sellers', {});
var listing = Listing.filter({limit: 1})[0]

how can I get listing.seller to equal the Seller object?

@AlJohri
Copy link
Author

AlJohri commented Nov 10, 2014

Ah, I didn't realize js-data docs applied to angular-data.

Reading http://www.js-data.io/v1.0.0-alpha-3-0/docs/relations I figured it out.

app.factory('Listing', ['DS', function (DS) {
  return DS.defineResource({
    name: 'listing',
    baseUrl: '/api/v1',
    deserialize: function(name, data) {
      return data.data.objects;
    },
    relations: {
      belongsTo: {
        seller: {
          localField: 'seller',
          localKey: 'sellerId'
        }
      }
    }
  });
}]);

app.factory('Seller', ['DS', function (DS) {
  return DS.defineResource({
    name: 'seller',
    baseUrl: '/api/v1',
    deserialize: function(name, data) {
      return data.data.objects;
    },
    relations: {
      hasMany: {
        listing: {
          localField: 'listings',
          foreignKey: 'sellerId'
        }
      },
    }
  });
}]);

@AlJohri AlJohri closed this as completed Nov 10, 2014
@jmdobry
Copy link
Member

jmdobry commented Nov 10, 2014

This kind of question would be better asked on the mailing list

@AlJohri
Copy link
Author

AlJohri commented Nov 10, 2014

thanks for letting me know!

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