Coder Social home page Coder Social logo

Comments (10)

patrixd avatar patrixd commented on July 21, 2024

I have the same problem. Did you fix the problem?

from backbone.subroute.

patrixd avatar patrixd commented on July 21, 2024

I have changed
var hash = Backbone.history.getHash();
to
var hash = Backbone.history.fragment;

And now It works

from backbone.subroute.

remkoboschker avatar remkoboschker commented on July 21, 2024

Hi,

I had the same issue and solved it by creating the subrouter in a handler of the main router. However this introduces a new problem. When I take a look at the Backbone.history object, I notice that when I reload the page with the url of any of the subroutes (except the empty subroute), that the handlers for the subroutes are no longer there!

For example in the main router:
routes: {
"": "index",
"records": "getRecords",
"records/": "getRecords"
},

getRecords: function () {
if (!this.recordsRouter) {
this.recordsRouter = new RecordsRouter("records");
}
}

In the subrouter:
routes: {
"": "showAllRecords",
":id": "showRecord"
}

When I go to localhost:8080/#records
and then to localhost:8080/#records/1
there is no problem (Backbone.history shows the subrouter handlers)

but when I go to localhost:8080/#records/1 again
then the subroutes are not triggered (Backbone.history does not show any subroute handlers)

reloading localhost:8080/#records does not cause any problems

Going straight to localhost:8080/#records/1 also does not trigger the subrouts (handlers not there) which probably comes down to the same thing as reloading.

Any suggestions?

from backbone.subroute.

remkoboschker avatar remkoboschker commented on July 21, 2024

Hello again,

It looks like I managed to get it sorted. In the piece of code that get called before anything else I have added:

app.router = new Router(); // this is the main router
Backbone.history.start({pushState:false, root: "/");
app.recordRouter = new RecordRouter({createTrailingSlashRoutes: true}); // this is the subrouter

I'll let you know if I run into any problems.

from backbone.subroute.

kfigiela avatar kfigiela commented on July 21, 2024

I don't use lazy loading and I couldn't get it working until I commented these lines:

        // grab the full URL
        // var hash = Backbone.history.getHash();

        // Trigger the subroute immediately.  this supports the case where 
        // a user directly navigates to a URL with a subroute on the first page load.
        // Backbone.history.loadUrl( hash );

I initialize subrouters in main router initialize and do Backbone.history.start after app and routers are initialized. Seems to work well!

from backbone.subroute.

moderndegree avatar moderndegree commented on July 21, 2024

I do lazy load my sub-routers and I discovered that having a catch-all in my sub-routers fixes the issue for me.

routes: {
"*hash" : "handleCatchAll"
}

from backbone.subroute.

geekdave avatar geekdave commented on July 21, 2024

Hi all,

The procedure I've been using is:

  1. Initialize a vanilla Backbone.Router to handle your base routes
  2. Call Backbone.History.start()
  3. Initialize SubRouters, either manually or automatically as part of a base route (for example, the first time someone navigates to "/books" you could have a main Router callback that initializes the Books SubRouter if not already initialized)

I, too, have noticed that #3 must come after #2. In my workflow this isn't a problem. Let me know if there are specific use cases that make this workflow problematic for anyone, and if possible include some code samples.

Also I just checked in a Jasmine test suite, so in the future it would be great to see a failing test spec for any reported issues. If I don't hear back on this issue in a couple weeks, I'll go ahead and close it with the above steps as the recommended course of action.

from backbone.subroute.

geekdave avatar geekdave commented on July 21, 2024

Robin Wenglewski [email protected]
2:19 AM (8 hours ago)
@geekdave I stumbled across this, too. It works with the workflow you
describe. However, I think usability will be greatly enhanced if we get it
to work so that initializing SubRouters works in Router.initialize.
This seems like the intuitive place for creating SubRouters.

Thanks Robin. I have considered creating an extension of Backbone Router
called BaseRouter whose purpose would be intended to create subrouters
based on the first part of a navigation path. I'm pretty swamped at work
so it might be awhile until I can get to this. In the meantime if you'd
like to help take a crack at the coding, I'd welcome a pull request.

Just noticed that your comment appears to have been deleted from the issues
list for some reason, so I've quoted your comment above.

Thanks,
Dave

On Tue, Nov 13, 2012 at 2:19 AM, Robin Wenglewski
[email protected]:

@geekdave https://github.com/geekdave I stumbled across this, too. It
works with the workflow you describe. However, I think usability will be
greatly enhanced if we get it to work so that initializing SubRouters works
in Router.initialize. This seems like the intuitive place for creating
SubRouters.


Reply to this email directly or view it on GitHubhttps://github.com//issues/3#issuecomment-10320044.

from backbone.subroute.

mohitmayank avatar mohitmayank commented on July 21, 2024

i just tried it myself and getting the same issue. too deep and too tight so cant put more details right now. but wudnt it be a good idea to somehow check if the history is not started and start it in the constructor of subroute?

from backbone.subroute.

evandegr avatar evandegr commented on July 21, 2024

I've noticed a little bit of strange behavior that I believe is related to this issue.

If I load a sub route i.e. 'route/sub' right off the bat, I notice that the subrouter does not get called. However, if I add an initialize onto my subrouter and do some kind of action there like a simple console.log, it DOES work.

// this code works
app.SocialStream.Router = Backbone.SubRoute.extend({
        initialize : function(){
            console.log('test');
        },
        routes: {
            'new' : 'newSocialStreamDashboard',
        },

        newSocialStreamDashboard : function() {
            alert('hells yes');
        }
    });
// this code doesn't
app.SocialStream.Router = Backbone.SubRoute.extend({
        routes: {
            'new' : 'newSocialStreamDashboard',
        },

        newSocialStreamDashboard : function() {
            alert('hells yes');
        }
    });
// this code also doesn't
app.SocialStream.Router = Backbone.SubRoute.extend({
        initialize : function(){

        },
        routes: {
            'new' : 'newSocialStreamDashboard',
        },

        newSocialStreamDashboard : function() {
            alert('hells yes');
        }
    });

Any idea why that might happen?

from backbone.subroute.

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.