Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. // This file lives in an npm module with the name of 'article-card' in a cards.js file.
  2. // (I think just using index.js might be a good idea too--i can go either way here)
  3.  
  4. const JSONAPIFactory = require('@cardstack/test-support/jsonapi-factory');
  5.  
  6. module.exports = function() {
  7. let factory = new JSONAPIFactory();
  8.  
  9. // the card ID specified only needs to speify the last part of the multi-part ID.
  10. // the rest of the card ID parts are derived from the package name.
  11. // (This assumes that a package only ever contains one card--not sure if that is a valid assumption)
  12. return factory.getDocumentFor(factory.addResource('card', 'millenial-puppies')
  13. .withAttributes({
  14. 'isolated-template': `
  15. <h1>{{this.title}}</h1>
  16. <h3>By {{this.author}}</h3>
  17. <ul>
  18. {{#each this.tags as |tag|}}
  19. <li>{{tag.id}}</li>
  20. {{/each}}
  21. </ul>
  22. <div>{{this.body}}</div>
  23. `,
  24. 'edit-template': `
  25. <h1>{{this.title}}</h1>
  26. <h3>By {{this.author}}</h3>
  27. <ul>
  28. {{#each this.tags as |tag|}}
  29. <li>{{tag.id}}</li>
  30. {{/each}}
  31. </ul>
  32. <div>{{this.body}}</div>
  33. `,
  34. 'embedded-template':`
  35. <h3>{{this.title}}</h3>
  36. <p>By {{this.author}}</p>
  37. `
  38. // This card will inherit the genesis card's js and css
  39. })
  40. .withRelated('fields', [
  41. factory.addResource('fields', 'title').withAttributes({
  42. 'is-metadata': true,
  43. 'needed-when-embedded': true,
  44. 'field-type': '@cardstack/core-types::string' //TODO rework for fields-as-cards
  45. }).withRelated('constraints', [
  46. factory.addResource('constraints')
  47. .withAttributes({
  48. 'constraint-type': '@cardstack/core-types::not-null',
  49. 'error-message': 'The title must not be empty.'
  50. })
  51. ]),
  52. factory.addResource('fields', 'body').withAttributes({
  53. 'is-metadata': true,
  54. 'field-type': '@cardstack/core-types::string'
  55. }),
  56. factory.addResource('fields', 'tags').withAttributes({
  57. 'is-metadata': true,
  58. 'needed-when-embedded': true,
  59. 'field-type': '@cardstack/core-types::has-many'
  60. }).withRelated('related-types', [
  61. // this is modeling an enumeration using a private model.
  62. // this content type name will be prefixed with the card's
  63. // package and card name, such that other cards can also
  64. // have their own 'tags' internal content types.
  65. factory.addResource('content-types', 'tags')
  66. ]),
  67. ])
  68. )
  69.  
  70. // The content type is actually derived from the card's multipart ID,
  71. // and the hub derives the content type as result of processing the
  72. // card's schema with the field definitions above. The developer
  73. // should not have to worry about card model content type schema,
  74. // as the hub will take care of constructing that with is why `withCardModel()`
  75. // has no params as its content type name and its ID are fully derived from
  76. // the card id.
  77. .withCardModel()
  78. .withAttributes({
  79. title: 'The Millenial Puppy',
  80. author: 'Hassan Abdel-Rahman',
  81. body: `
  82. It can be difficult these days to deal with the
  83. discerning tastes of the millenial puppy. In this
  84. article we probe the needs and desires of millenial
  85. puppies and why they love belly rubs so much.
  86. `
  87. })
  88. .withRelated('tags', [
  89. // Note that the tags models will be prefixed with this card's ID
  90. // such that you will never run into model collisions for tags
  91. // of different article cards
  92. factory.addResource('tags', 'millenials'),
  93. factory.addResource('tags', 'puppies'),
  94. factory.addResource('tags', 'belly rubs'),
  95. ])
  96. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement