Skip to content

Access models related via hasAndBelongsToMany#29

Merged
bajtos merged 6 commits into
masterfrom
feature/get-related-models
Apr 7, 2014
Merged

Access models related via hasAndBelongsToMany#29
bajtos merged 6 commits into
masterfrom
feature/get-related-models

Conversation

@bajtos

@bajtos bajtos commented Apr 3, 2014

Copy link
Copy Markdown
Member

The relation "Product hasAndBelongsToMany Category" creates a scope
property on the Product model with three methods: get, create, delete.

At the Angular side, these methods are defined on the Product $resource,
therefore they return the result as Product instead of Category.

The solution presented in this patch has two main parts:

  1. The Category $resource has internal methods calling remote (REST)
    Product.categories.* methods. This way the result is returned
    as Category.
  2. The Product $resource has extra properties (methods) with hand-coded
    implementation delegating to the internal Category methods.

Methods & delegations:

  • Product.categories() -> Category['::get::Product::categories']()
  • Product.categories.create() -> Category['::create::Product::categories']()
  • Product.categories.destroyAll() -> Category['::delete::Product::categories']()

/to @ritch please review.

Closes #26.
Depends on strongloop/loopback#223 and loopbackio/loopback-datasource-juggler#96.
Related: loopbackio/loopback-datasource-juggler#97, loopbackio/loopback-datasource-juggler#95

@slnode

slnode commented Apr 3, 2014

Copy link
Copy Markdown

Test Failed. To trigger a build add comment - ".test\W+please"
Refer to this link for build results: http://ci.strongloop.com/job/loopback-angular/149/

@slnode

slnode commented Apr 3, 2014

Copy link
Copy Markdown

Test Failed. To trigger a build add comment - ".test\W+please"
Refer to this link for build results: http://ci.strongloop.com/job/loopback-angular/150/

@bajtos

bajtos commented Apr 4, 2014

Copy link
Copy Markdown
Member Author

test please

@slnode

slnode commented Apr 4, 2014

Copy link
Copy Markdown

Test Failed. To trigger a build add comment - ".test\W+please"
Refer to this link for build results: http://ci.strongloop.com/job/loopback-angular/158/

Comment thread lib/services.js

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Maybe include the version numbers in here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I would include it if the version was easily available. I mean all I have is the app object here. I could probably hack the Node module system to find out what module created the app object and then where the loopback-datasource-juggler is installed, so that I can read its package.json file and get the version, but considering the complexity, I'd rather avoid that.

@ritch

ritch commented Apr 4, 2014

Copy link
Copy Markdown
Member

Looks like the build is failing... so that should be fixed. Had a few minor comments that don't really need to be addressed as part of this PR. Overall LGTM.

@bajtos

bajtos commented Apr 4, 2014

Copy link
Copy Markdown
Member Author

Thanks @ritch. The build is failing with a strange error and was failing before this PR too :(

Miroslav Bajtoš added 6 commits April 7, 2014 15:50
Extract the template of ngdoc comment for a resource method into
a function.

No changes of functionality.
Sample usage:

// data for the POST /setup request
JSON.stringify({
  models: {
    Product: { properties: { name: 'string' } },
  },
  setupFn: (function(app, cb) {
    app.models.Product.create(
      { name: 'a-product' },
      function(err, product) {
        if (err) return cb(err);
        cb(null, { productId: product.id }
      });
  }).toString()
})

The server will run the provided function and wait until the callback
is called. Data passed to the callback are exposed to browser tests
as "testData" and can be accessed via the angular injector:

  $injector.get("testData").productId
The LoopBack keeps all models in a static registry. When two tests
are using the same model name, some of the model classes may get shared,
causing bugs difficult to debug.

This commit modifies "/setup" handler to reset the static registry
to the initial state every time a new application is created.
The relation "Product hasAndBelongsToMany Category" creates a scope
property on the Product model with three methods: get, create, delete.

At the Angular side, these methods are defined on the Product $resource,
therefore they return the result as Product instead of Category.

The solution presented in this commit has two parts:

 1. The Category $resource has internal methods calling remote (REST)
    Product.categories.* methods. This way the result is returned
    as Category.

 2. The Product $resource has extra properties (methods) with hand-coded
    implementation delegating to the internal Category methods.

Methods & delegations:
  Product.categories()
    -> Category['::get::Product::categories']()

  Product.categories.create()
    -> Category['::create::Product::categories']()

  Product.categories.destroyAll()
    -> Category['::delete::Product::categories']()

The original scope methods (e.g. prototype$__get__{scope}) are marked
as deprecated, the deprecation text includes the name of the new method
to use.
The URL of prototype methods includes sharedCtor parameters like ":id",
for example

  GET /api/Products/:id/categories

Because all $resource actions are static (non-prototype) in ngResource,
callers has to include sharedCtor parameters in the arguments.

  Product.categories({ id: productId });

This commit modifies the generated ngdoc to include sharedCtor
parameters in actions for prototype methods.
@slnode

slnode commented Apr 7, 2014

Copy link
Copy Markdown

All is well
Refer to this link for build results: http://ci.strongloop.com/job/loopback-angular/168/

bajtos added a commit that referenced this pull request Apr 7, 2014
Access models related via hasAndBelongsToMany
@bajtos
bajtos merged commit b312d70 into master Apr 7, 2014
@bajtos
bajtos deleted the feature/get-related-models branch April 7, 2014 14:43
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

Successfully merging this pull request may close these issues.

Issue with related models export

3 participants