BackboneJS + Codeigniter pushState true not working

假如想象 提交于 2019-12-25 16:55:29

问题


So I have this Backbone App where I use Codeigniter for the Backend. For some reason, pushState:true does not work.

So, my main.js of my backbone app has this:

Backbone.history.start({ pushState: true, root: App.ROOT });

My app.js has this:

var App = {
    ROOT: '/projects/mdk/'
};

and my navigation module, which renders the menulinks, each item has this:

this.insertView(new ItemView({
model: new Navigation.ItemModel({
    href: App.ROOT + 'home',
    class: 'home',
    triggers: 'home',
    route: this.route
   })
}));

and the model for it:

Navigation.ItemModel = Backbone.Model.extend({
    defaults: {
        href: '',
        text: '',
        triggers: [],
        route: ''
    }
});

All I get from this is "Page not found"...

Add: When I in the view change it to href:'#news' - it works, but it dont really makes sense...

Anyone who knows the issue here?


回答1:


From the documentation (http://backbonejs.org/#History):

Note that using real URLs requires your web server to be able to correctly render those pages, so back-end changes are required as well. For example, if you have a route of /documents/100, your web server must be able to serve that page, if the browser visits that URL directly.

The problem is that your server isn't responding to whatever URL your app is on. For every URL that your Backbone app can reach, your server MUST return a valid HTML page (contianing your Backbone app).




回答2:


ok I found a solution by myself:

I made this hack:

$(document).on('click', 'a:not([data-bypass])', function (evt) {

    var href = $(this).attr('href');

        if (href && href.indexOf('#') === 0) {
            evt.preventDefault();
            Backbone.history.navigate(href, true);
        }
    });

and then I made:

href: '#home',

That solved the problem, now evereythings runs fluently..



来源:https://stackoverflow.com/questions/21779544/backbonejs-codeigniter-pushstate-true-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!