Giter Club home page Giter Club logo

Comments (6)

millermedeiros avatar millermedeiros commented on July 20, 2024

crossroads already have a method called create so this method should have a different name.

when I was starting crossroads development I considered adding id/name to each route but I thought it would be redundant in most cases (I never really needed it) but maybe we should add a new method to get the route by id and add a new method to the Route itself to trigger it - keeping ID as an optional property (use the route pattern as id if not present), e.g:

crossroads.addRoute('/news/{id}', showNews);
var r = crossroads.addRoute('/projects/{id}/:name:', showProjectDetail);
r.id = 'projects';

// get route by ID
crossroads.getRoute('projects').trigger({id:123, name:'foo-bar'});

//can get route by pattern (maybe only if it doesn't have an ID)
crossroads.getRoute('/news/{id}').trigger({id:15});

Issue #34 is also related.

PS: I probably won't have time to implement it till next year (if I do)

Thanks for reporting.

from crossroads.js.

millermedeiros avatar millermedeiros commented on July 20, 2024

it's important to remind that I'm not a huge fan of using strings since it can lead to bugs that are hard to track.. I would recommend you to keep references to every Route you may need to call and create a new method for each one of them, e.g.:

// keep route as a private variable
var projectsRoute = crossroads.addRoute('/projects/{id}');
...
// than expose a method on your API
myNamespace.goToProjects = function (paramObj) {
  projectsRoute.trigger(paramObj);
};

another option is to expose all the routes directly:

myNamepace.routes = {
  projects : projectsRoute,
  news : newsRoute
};

//than you can do later
myNamespace.routes.projects.trigger({id:123});

This means that the getRoute() method may never ship in crossroads while trigger() is a very strong candidate.

from crossroads.js.

millermedeiros avatar millermedeiros commented on July 20, 2024

just got back to this feature request and realized that I forgot to add the simplest way to "trigger" a route, since the matched is simply a Signal you can dispatch is at will:

var route = crossroads.addRoute('/foo/{bar}', function(id){
  console.log(id);
});
route.matched.dispatch(123);

you can also dispatch the crossroads.routed signal if needed.

from crossroads.js.

milworm avatar milworm commented on July 20, 2024

Cool, many thanks

from crossroads.js.

millermedeiros avatar millermedeiros commented on July 20, 2024

Ignoring this feature request since user can dispatch route.matched signal if needed. To do it properly would require validating each argument to see if it matches the route.rules. It would be also strange to dispatch the crossroads.routed signal if it really didn't routed anything (it was triggered manually), so I think it's better to keep this feature out to avoid confusions.

I should add route.interpolate() (#34) in the next release so it will be easy to call crossroads.parse passing the proper string like this:

var route = crossroads.addRoute('/foo/{id}');
var req = route.interpolate({id: 123}); // "/foo/123"
crossroads.parse( req );

Cheers.

from crossroads.js.

millermedeiros avatar millermedeiros commented on July 20, 2024

@milworm route.interpolate() just landed on v0.9.0, cheers.

from crossroads.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.