locked
Currently metro apps are not DRY RRS feed

  • General discussion

  • I am looking through all the boyerplate code you have to repeat for every general task like adding a new fragment page.

     

     

    // Custom event raised after the fragment is appended to the DOM.
        WinJS.Application.addEventListener('fragmentappended', function handler(e) {
            if (e.location === '/html/error.html') { fragmentLoad(e.fragment, e.state); }
        });
    
    
    WinJS.Namespace.define('landingPage', {
            fragmentLoad: fragmentLoad,
            itemInvoked: itemInvoked
    <br/> });
    
    How could we reduce this amount of boilerplate code?

    Its just that when I look at the examples it feels the DRY principle is violated somehow.

    I have no solution at hand right now, but I would prefer an inverse event propagation system like the White-Board-Pattern used in OSGi.

    Then you would have one (!) "fragmentappended" listener that would look if there are FragmentListeners in the system, that would like to be informed about the event. The listener would just register themself like this:

    reg = registry.register({
      'fragmentappended/error': function(elements, options) {
        var item = options && options.item ? options.item : getItem();
        elements.querySelector('.title').textContent = item.title;
      },
      
      getItem: function() {
        return {};
      }
    });
    

    See there is no more "WinJS.UI.processAll". This is all handled by the registry when it catches the "fragmentappended" event.

    The registry would have this pseudocode:

    WinJS.Application.addEventListener('fragmentappended', function handler(e) {
            if (e.location === matches registered listener) { 
               listenerMethod = getRegisteredListenerMethod();
               WinJS.UI.processAll(e.fragment).then(function() {
                 listenerMethod(e.fragment, e.state); 
               }
            }
        });
    


    This would remove the burden from the fragments. And you could implement all event listening code in nicely wrapped classed.

    What you think?

     

    Wednesday, November 16, 2011 7:09 PM