Problems routing with Codeigniter and Backbone.js

Here are the frameworks that I use:

  • Codeigniter
  • Codeigniter REST API
  • Require.js
  • Backbone.js

Application Launch:

  • The htaccess file redirects everything through the Codeigniter index.php file.
  • Codeigniter has a default route setting to invoke the Start controller.
  • The start controller calls Start View (the only view in Codeigniter).
  • The Start view stores the template for the entire website and calls Require.js
  • The .js requirement is met. The backbone and application work as expected.

Problem: Routing to other types of baseline:

  • Now I want to direct through Backbone to other "types" of Backbone.
  • The problem is that when I try to provide a link, for example "localhost / otherPage" in HREF, the htaccess file is launched, and Codeigniter tries to find this controller, and I get 404 generated by Codeigniter.
  • I tried to make a route in Backbone instead of HREF using `app.navigate (" otherPage ", {trigger: true});` but I get the same 404 error generated by Codeigniter.

Which direction should be used:

  • I would like to continue to use Codeigniter for its RESTFUL API, models and some special validation functions.
  • I also want to use Backbone and Require on the interface.
  • Can I change the htacess redirection to an index.html file that runs Require and Backbone. Will the Codeigniter RESTFUL API Controllers still work correctly if I use this approach?
  • Do I create a view in Codeigniter for each page and each page launches "Require + Trunk" for each view?
  • I will look into Node.js. soon Should I just destroy the aspect of the Codeigniter framework and go the whole pig on Node.js?

Any guidance is greatly appreciated.

+6
source share
2 answers

If you have not already done so, you may need to route through the Backbone through hashtag changes (this is its normal behavior, pushState: false), since changing the hashtag will in no way lead to a server call, thus ignoring the Codeigniter Router.
In your example, you would like to switch to localhost / # otherPage.
Then use the Codeigniter router for ajax REST calls.

+3
source

Another way to prevent your browser from sending an HTTP request using the HREF link is to simply override it using javascript and jquery. It may be useful if you do not want to always use hashtags, as Loamhoof suggested.

Example:

$('#linkID').on('click', function(event) { event.preventDefault(); backboneRouter.navigate($(event.currentTarget).attr('href'), { trigger:true }); }); 
+1
source

All Articles