Closed alpual closed 5 years ago
Thanks for clarifying. I agree, it is more clear with the parens.
On Mon, Mar 25, 2019, 5:13 PM K Adam White notifications@github.com wrote:
@kadamwhite commented on this pull request.
In using-the-rest-api/backbone-javascript-client.md https://github.com/WP-API/docs/pull/42#discussion_r268890653:
+```js +// Extend wp.api.models.Post and wp.api.collections.Posts to load a custom post type +const CustomPost = wp.api.models.Post.extend( {
- urlRoot: wpApiSettings.root + 'wp/v2/custom_post_slug',
- defaults: {
- type: 'custom_post_slug',
- }, +} ); +const CustomPosts = wp.api.collections.Posts.extend( {
- url: wpApiSettings.root + 'wp/v2/custom_post_slug',
- model: BLProduct, +} ); +const someCustomPosts = new CustomPosts; +someCustomPosts.fetch().then( ( posts ) ) => {
- // do something with the custom posts +}
Incidentally, you're correct that the code as you wrote it works: [image: image] https://user-images.githubusercontent.com/442115/54960084-af9ffb80-4f31-11e9-9e92-e41ff7224ae6.png However this is a one-off exception to the rule within JavaScript (stack overflow link) https://stackoverflow.com/a/3034952 which allows constructor functions to be called with no parentheses if no arguments need to be passed. Because a constructor is a function (even if defined using the new class keyword), and because every other kind of function call requires parentheses, in documentation I hold that it is always preferable to use the more general & consistent standard syntax to avoid confusing readers unfamiliar with the exception.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/WP-API/docs/pull/42#discussion_r268890653, or mute the thread https://github.com/notifications/unsubscribe-auth/ANLGNazJx-dlYViTzxPRFtxQ8WQlkMUBks5vaVgjgaJpZM4bEYS3 .
I found it difficult to work this out with the documentation here. This may not be the best or the most "modern" way to achieve this, but it works for me. Definitely open to suggestions to improve this code.