Code Monkey home page Code Monkey logo

Comments (8)

crapthings avatar crapthings commented on July 17, 2024

same here, i think it works with path like /posts/:_id if there's no post's id found it renders notFoundTemplate

but if there're no path pre define in Router.map it wont work right ?

from iron-router.

yubozhao avatar yubozhao commented on July 17, 2024

It works on randomness URL that wasn't setup in Router.map.

Any code of your example?

If you can duplicate the case you encountered in a test repo, it will be easier to find the problem.

from iron-router.

cmather avatar cmather commented on July 17, 2024

Add a catch all route at the end of your routes with a path of '*'. The template will be the same notFoundTemplate. Inside of an existing route, if the data function or value is falsy and you've specified a notFoundTemplate the render method will automatically render it.

Sent from my iPhone

On Aug 19, 2013, at 7:05 AM, crapthings [email protected] wrote:

same here, i think it works with path like /posts/:_id if there's no post's id found it renders notFoundTemplate

but if there're no path in Router.map it wont work right ?


Reply to this email directly or view it on GitHub.

from iron-router.

cmather avatar cmather commented on July 17, 2024

Let me expand from my iPhone response above. There are two cases for a notFoundTemplate: 1) The route exists but there is no record found given the current route (e.g. /posts/5 and post 5 does not exist); and 2) The route does not exist at all (e.g. /bogus/route).

Case 1: Data not found: The render method takes care of this automatically, as long as a notFoundTemplate is defined on the route or RouteController. It determines whether a record is found by checking whether your data function or value returns a falsy value. For example:

Router.map(function () {
  // this route will always render the notFound template because the data function always returns false.
  this.route('post', {
    path: '/posts/:id',
    data: function () { return false; },
    notFoundTemplate: 'notFound'
  });
});

Case 2: Route does not exist: You need to define a catch all route at the end. This is probably something that should be handled automatically by the router given you pretty much do it every time.

Router.map(function () {

  /* all of your actual routes go here */

  /* at the bottom define your catch all route */

  // matches all urls but doesn't get called until all previous routes have been tested
  this.route('notFound', {path: '*'});
});

from iron-router.

cmather avatar cmather commented on July 17, 2024

If someone could update the example and README that would be awesome :-).

from iron-router.

yubozhao avatar yubozhao commented on July 17, 2024

Will do after computer comes back.

from iron-router.

oglethorp avatar oglethorp commented on July 17, 2024

Thanks @cmather for the timely response. Works great!

from iron-router.

crapthings avatar crapthings commented on July 17, 2024

ah nice it works.

from iron-router.

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.