AlexGalays / abyssa-js

Framework agnostic hierarchical router for single page applications
MIT License
71 stars 9 forks source link

NotFound exception thrown when State has enter, exit and sub State #31

Closed LucaColonnello closed 9 years ago

LucaColonnello commented 9 years ago

Check this jsBin: http://jsbin.com/zomimotiyu/1/

var State = Abyssa.State,
    Router = Abyssa.Router,
    router;

  router = Router({
    index: State('/', function() { console.log( "enter index" ); }),

    books: State('/books', {
      enter: function() {
        console.log( "enter books index" );
      },
      exit: function(){
        console.log( "exit books index" );
      },

      detail: State(':id', {
         enter: function(params) {
           console.log( "enter books detail", params );
         },
         exit: function(params){
           console.log( "exit books detail", params );
         }
      })
    })
})
.configure({
    enableLogs: true,
    urlSync: true
})
.init('index');

Going on "/books" it doesn't match books state. I tried to use an internal state booksIndex url at "" and it works!

I have to make books State parent of bookDetail and it must enter when url is "/books", do some stuff and when I click for a book detail the url must be "/books/2" and it must not exit the books State.

How can I do this?

LucaColonnello commented 9 years ago

Update: I have set this to the books State config:

var State = Abyssa.State,
    Router = Abyssa.Router,
    router;

  router = Router({
    index: State('/', function() { console.log( "enter index" ); }),

    books: State('/books', {
      enter: function() {
        console.log( "enter books index" );
      },
      exit: function(){
        console.log( "exit books index" );
      },

      index: State(''),

      detail: State(':id', {
         enter: function(params) {
           console.log( "enter books detail", params );
         },
         exit: function(params){
           console.log( "exit books detail", params );
         }
      })
    })
})
.configure({
    enableLogs: false,
    urlSync: true
})
.init('index');

It Works!

It is not a great solution for code cleaning. If this could be solved differently, it will be great!

AlexGalays commented 9 years ago

Hello LucaColonnello, you actually found the solution by yourself. Only leaf states can be transitionned to (it should be mentionned in the README). If all combinations were possible, there would be many conflicts and unresolvable paths especially when reverse-routing (router.link)

I hope it answers your question.

LucaColonnello commented 9 years ago

It will be possible to create the empty route Abyssa itself? Or if not, can make sense?

AlexGalays commented 9 years ago

Maybe it's possible to create the default, empty route if there is no conflicting route for that parent already, yes. I need to think about it.

LucaColonnello commented 9 years ago

Ok, let me know, this pattern is the obviously better solution for HMVC application, preventing some interaction problem and limitation.

LucaColonnello commented 9 years ago

I saw that You have did this update on 7.0.0. Is just what I saw?

State( "url/url/url", {  enter: ... , exit: ... }, { children: ..., children: ... } )
AlexGalays commented 9 years ago

I'm not sure I'm following.

That change in 7.0.0 just brings a different way to declare a state and its hierarchy; it doesn't introduce some kind of default state for each level of a hierarchy like you wanted, if I understood correctly.

AlexGalays commented 9 years ago

I think it's a good idea to implement this (although only future usage will confirm) so I'm going to work on it; A few other changes are needed to make this work though.

LucaColonnello commented 9 years ago

Yeah, I think! It would be great if You implements either guards and parameter's validation.

With guards you can apply policy and condition to allows route entering.

Il sabato 9 maggio 2015, AlexGalays notifications@github.com ha scritto:

I think it's a good idea to implement this (although only future usage will confirm) so I'm going to work on it; A few other changes are needed to make this work though.

— Reply to this email directly or view it on GitHub https://github.com/AlexGalays/abyssa-js/issues/31#issuecomment-100476633 .

Luca Colonnello 340 8707406 luca.colonnello91@gmail.com

AlexGalays commented 9 years ago

Implemented in 7.0.1