Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Structuring large code bases with small re-usab...

Structuring large code bases with small re-usable components

From JSUnconf

Bastian Hofmann

April 26, 2014
Tweet

More Decks by Bastian Hofmann

Other Decks in Programming

Transcript

  1. de duplication ode duplication code duplication code duplication code duplication

    code duplication code duplication code duplication code duplication code duplication code duplication code duplication code duplication code duplicatio code duplicat code duplic code dup code du code cod co co
  2. PHP

  3. MVC

  4. Profile Publications Publication Publication Publication AboutMe LeftColumn Image Menu Account

    Account Account Account Account Publication1 Publication2 Publication3 Institution
  5. Widget Widget Widget Widget Preparer Resolver Resolver Services Connector Interfaces

    Connector Implementations Batch requirements and pass them to resolvers
  6. Widget Widget Widget Widget Preparer Resolver Resolver Services Connector Interfaces

    Connector Implementations Call Services as effective as possible (Multi-GET,...)
  7. Widget Widget Widget Widget Preparer Resolver Resolver Services Connector Interfaces

    Connector Implementations Attach fetched data to Requirements and pass them back to the preparer
  8. Widget Widget Widget Widget Preparer Resolver Resolver Services Connector Interfaces

    Connector Implementations Distribute fetched data to the widgets that required it
  9. public function collect() {
 yield [
 new EntityRequirement(
 'account',
 Account::class,


    ['id' => $this->request->get('id')]
 ),
 ];
 
 yield [
 new ServiceRequirement(
 'scienceDisciplines',
 AccountService::class,
 'getScienceDisciplines',
 ['account' => $this->account]
 )
 ];
 }
  10. class PublicationKeywordSearch {
 
 public $publications;
 public $publicationListItems = [];


    
 public function collect() {
 yield [
 serviceRequirement(
 'publications',
 PublicationService::getCall()->getByKeywords(
 ['virology', 'cancer'], 10, 0
 )
 )
 ];
 foreach ($this->publications as $publication) {
 yield new WidgetRequirement(
 'publicationListItems',
 PublicationItem::CLASS,
 [ 'publicationId' => $publication->getId(), 'publication' => $publication ]
 );
 }
 }

  11. class PublicationItem {
 
 public $publicationId;
 public $publication;
 
 public

    function collect() {
 yield new RequestDataRequirement('publicationId');
 yield [
 new EntityRequirement(
 'publication',
 Publication::class,
 ['id' => $this->publicationId]
 )
 ];
 }
 }
  12. <div id="{{widgetId}}">
 <h3>
 Publications 
 <a href="javascript:" class="js-showAll">
 ({{count}})
 </a>


    </h3>
 <ul>
 {{#publicationItems}}
 <li>{{{.}}}</li>
 {{/publicationItems}}
 </ul>
 {{#showLegalFooter}}
 {{{legalFooter}}}
 {{/showLegalFooter}}
 </div>
  13. <div id="{{widgetId}}">
 <h3>
 Publications 
 <a href="javascript:" class="js-showAll">
 ({{count}})
 </a>


    </h3>
 <ul>
 {{#publicationItems}}
 <li>{{{.}}}</li>
 {{/publicationItems}}
 </ul>
 {{#showLegalFooter}}
 {{{legalFooter}}}
 {{/showLegalFooter}}
 </div>
  14. YUI.add('views.publicationList', function (Y) {
 Y.views.publicationList = Y.Base.create(Y.WidgetView, {
 events :

    {
 '.js-showALl' : {
 click : 'showALl'
 }
 },
 showAll : function () {
 var node = this.get('container');
 
 node.addClass('loading');
 
 Y.loadWidget(‚/publications/all', {
 keywords : this.data.keywords
 }, function (widget) {
 widget.render({
 replace : node
 });
 });
 }
 });
 });
  15. <polymer-element name="tk-element" attributes="owner color">
 <template>
 <span>This is <strong>{{owner}}</strong>'s tk-element.</span>
 <span

    id="el" style="color: {{color}}">Not ready...</span>
 </template>
 <script> 
 Polymer('tk-element', {
 owner: "Bastian",
 color: "red",
 ready: function() {
 this.$.el.innerText = this.owner + " is ready!";
 }
 });
 </script>
 </polymer-element>
  16. Profile Publications Publication Publication Publication AboutMe LeftColumn Image Menu <div

    id="placeholder"></div> <script>loadWidget('/aboutMe', function(w) { w.render({ replace : '#placeholder' }); })</script> Institution