YahooArchive / fluxible-router

MOVED TO FLUXIBLE REPO
104 stars 26 forks source link

NavigateAction and previous route #85

Closed sAbakumoff closed 8 years ago

sAbakumoff commented 9 years ago

My app is widely using NavLink and NavigateAction. In some pages I would like to display the link to the "parent page", for example if a user views the Customers List page and clicks the link that opens the specific Customer page, I would like to display the link that leads back to "Customers List". RouteStore only exposes the current route, but not a previous route, so I have to use the following work-around for now for the flexible and once-for-all solution:

var RouteStore = require('fluxible-router').RouteStore;
var routes = require('../configs/routes');

function StaticRouteStore() {
    RouteStore.apply(this, arguments);
}

StaticRouteStore.routes = routes;

var TempFn = function () {};
TempFn.prototype = RouteStore.prototype;
StaticRouteStore.prototype = new TempFn();
StaticRouteStore.prototype.constructor = StaticRouteStore;

StaticRouteStore.storeName = RouteStore.storeName;

StaticRouteStore.handlers = Object.assign(RouteStore.handlers, {
    NAVIGATE_CLICK : '_handleNavigateClick'
});

StaticRouteStore.prototype.getRouteBeforeClick = function(payload){
    return this._routeBeforeClick;
};

StaticRouteStore.prototype._handleNavigateClick = function(payload){
    this._routeBeforeClick = payload;
};

StaticRouteStore.prototype.getRoutes = function () {
    return Object.assign({}, StaticRouteStore.routes, this._routes || {});
};

module.exports = StaticRouteStore;
var dispatchMessageAction = function(context, payload, done) {
  context.dispatch(payload.messageName, payload.messageBody, done);
};

var routeStore = context.getStore('RouteStore');
  if (routeStore && routeStore.getCurrentRoute) {
    var currentRoute = routeStore.getCurrentRoute();
    context.executeAction(dispatchMessageAction, {
      messageName : 'NAVIGATE_CLICK',
      messageBody : currentRoute
   })
}
        if(routeStore && routeStore.getRouteBeforeClick){
            var parentRoute = routeStore.getRouteBeforeClick();
            if(parentRoute){
                // add another NavLink that leads to the prev route.
            }
        }

Though it does not seem to be very elegant to me...Could anyone comment on this? Is it OK solution? Is there better approach? Thanks!

mridgway commented 9 years ago

I think this would be more useful to represent as nested routes which is requested in https://github.com/yahoo/fluxible-router/issues/72. Using the previous route is not always going to be accurate.