Public
Snippet $22 authored by Sebastien Robin

simple route (a big fat due to addition of listenHashChange and getCommandUrlFor)

Edited
/*global window, rJS */
/*jslint nomen: true, indent: 2 */
(function (window, rJS) {
  "use strict";

  function listenHashChange(gadget) {
    // Handle hash in this format: #$path1/path2?a=b&c=d
    function extractHashAndDispatch(evt) {
      var hash = (evt.newURL || window.location.toString()).split('#')[1],
        split,
        command = "",
        query = "",
        subhashes,
        subhash,
        keyvalue,
        index,
        args = {};
      if (hash !== undefined) {
        split = hash.split('?');
        command = split[0] || "";
        query = split[1] || "";
      }
      subhashes = query.split('&');
      for (index in subhashes) {
        if (subhashes.hasOwnProperty(index)) {
          subhash = subhashes[index];
          if (subhash !== '') {
            keyvalue = subhash.split('=');
            if (keyvalue.length === 2) {
              args[decodeURIComponent(keyvalue[0])] = decodeURIComponent(keyvalue[1]);
            }
          }
        }
      }

      return gadget.renderApplication({
        method: command[0],
        path: command.substr(1),
        args: args
      });

    }

    var result = loopEventListener(window, 'hashchange', false,
                                   extractHashAndDispatch),
      event = document.createEvent("Event");
    event.initEvent('hashchange', true, true);
    event.newURL = window.location.toString();
    window.dispatchEvent(event);
    return result;
  }

  rJS(window)
    .ready(function (gadget) {
      gadget.props = {
        start_deferred: RSVP.defer()
      };
    })

    .declareMethod("getCommandUrlFor", function(options) {
      var prefix = '?',
        result,
        key;
      result = "#";
      for (key in options) {
        if (options.hasOwnProperty(key) && options[key] !== undefined) {
          // Don't keep empty values
          result += prefix + encodeURIComponent(key) + "=" + encodeURIComponent(options[key]);
          prefix = '&';
        }
      }
      return result;
    })

    .declareMethod('route', function (options) {
      var gadget = this,
          args = options.args;

      var page;
      console.log("router.route is called with args", args);
      if (args.jio_key === undefined) {
        page = args.page || 'week_view';
        return {
          url: "gadget_work_report_" + page + ".html",
          options: args
        };
      } else {
        page = args.page || 'view';
        return gadget.get(args.jio_key)
          .push(function (doc) {
            var base_portal_type = doc.portal_type.toLowerCase().replace(/\s/g, "_");
            return {
              url: "gadget_work_report_" + base_portal_type + "_" + page + ".html",
              options: args
            }
          });
      }
    })

    .declareAcquiredMethod('renderApplication', 'renderApplication')
    .declareMethod('start', function () {
      this.props.start_deferred.resolve();
    })
    .declareService(function () {
      var gadget = this;
      return new RSVP.Queue()
        .push(function () {
          return gadget.props.start_deferred.promise;
        })
        .push(function () {
          // console.info('router service: listen to hash change');
          return listenHashChange(gadget);
        });
    });

}(window, rJS));