Backbone pushstate not catching routes, settting "root" properly - backbone.js

I think I'm missing something basic but I can't find out what, I've checked other post on this topîc but couldn't find a clear answer.
The url launching my app is:
http://local.myapp.ngx:8080/myapp/fileasset/app.html
app.html is loading require/main.js
// MAIN.JS
initializing the router:
new Router();
starting history:
Backbone.history.start({
pushState: true,
root: "/myapp/fileasset/"
});
// ROUTER.JS
routes: {
'':'test'
}
==> PROBLEM , test function is never called.
if I try this:
routes: {
'*wtv':'test'
}
==> THEN "test" is triggered but how to catch other routes ?
thanks for your help !

Indeed it was a really really basic thing, I didn't put the right root
root: "/myapp/fileasset/ui.html"

Related

Backbone Route not firing

This should be super basic but I can't get routing working. I should mention that the application is located in a subdirectory called /dist/. Here's my code:
var QuestionRouter = Backbone.Router.extend({
routes: {
"/dist/" : "startTest"
"dist/:id": "getModel"
},
startTest: function(){
console.log('home called')
},
getModel: function(){
app.getModel(id);
}
});
var app = new QuestionView;
var appRouter = new QuestionRouter;
Backbone.history.start({pushState: true});
The URL to trigger this route is:
www.example.com/dist/
www.example.com/dist/12345
Any help would be appreciated.
You'll need to use # (hash symbol).
Backbone routers are used for routing your applications URL's when
using hash tags(#)
This is a quote from a Backbone tutorial: What is a router?
See Backbone's Router documentation
Then your routes would be:
www.example.com/#/dist/
www.example.com/#/dist/12345
You can also use Backbone routes without hashes.
Ok So I was able to work this out:
My route hash should look like this:
routes: {
"" : "startTest",
":id": "getModel"
}
I had to remove pushState: true, with this in place the route wasn't being triggered, not sure why:
Backbone.history.start();

how to add a route to swagger-ui

I've been trying to add a route (health check) to swagger-ui. But haven't been able to figure out what is going on?
I added a routes object to SwaggerUi.js:
routes: {
'health': 'health'
},
health: function() {
return "OK";
}
And to the index.html file in src/main/html
I added: window.swaggerUi.on('route:health', function() { log('here'); });
But when I navigate to /health it could not GET. Has anyone done anything like this before?
This is not possible because swagger-UI is just raw HTML that serves up JS.

Backbone pushState and Laravel 4 - Routes won't work

I've got a very simple setup of laravel 4 with 1 controller for trips.
Now another simple setup of Backbone with a Router and routes to regular routes like trips, trips/create. I want to use pushState: true to have nice URL but I can't seem to get it to work. When I try to reach the URLs it sends me to the page served by the server with my json data.
However, if I type: www.mysite.com/#trips I am "redirected" to www.mysite.com/trips and then my method for this specific route triggers.
Here's my Router:
App.Router = Backbone.Router.extend({
routes: {
'': 'index',
'trips': 'trips',
'trips/create': 'newTrip',
'trips/:id': 'showTrip'
},
index: function(){
},
trips: function(){
console.log('All trips') ;
},
newTrip: function(){
console.log('New trip') ;
},
showTrip: function(id){
console.log('trips id:' + id) ;
}
});
This is probably caused by the Backbone router not being able to match the URL to your Backbone routes. To debug, add this to your routes:
'*actions': 'defaultAction'
Then in the router add:
defaultAction: function(path) {
console.log('Route "' + path + '" not defined, redirecting to homepage!');
}
You will probably see that the path is different from anything in your routes. To fix this, you need to tell Backbone the root of your paths. You do this while activating pushstates.
Backbone.history.start({
pushState: true,
root: 'http://mydomain.com/myapplication/'
});
Hope this helps.

Backbone Router needs to ignore "index.html"

I'm not in a position to modify the htaccess of the server and want to use Backbone Router.
The problem is I'm stuck with a static html file in the url (index.html)
How do I get Backbone to ignore the index.html?
Thanks in advance.
var Router = Backbone.Router.extend({
routes : {
"page-one" : "pageOne"
},
pageOne : function(){},
pageTwo : function(){},
pageThree : function(){}
});
scn.router = new Router();
Backbone.history.start({root: "/Backbone/index.html"});
I had on pushState, which I don't need. That was THE problem. For now.
pushState was the problem. I turned it off then my issues were solved.

Backbone.js Pushstate: true not returning callback function

I am pretty new to backbone js and I am having some problem getting the pushstate functionality of my app to work. Here is an eg of my route:
var TodoRouter = new (Backbone.Router.extend({
routes: {
"": "index",
"item/add": "AddTodoItem",
"list/add": "AddTodoList"
},
AddTodoItem: function() {
//e.preventDefault();
alert("add new item");
},
AddTodoList: function(e) {
//e.preventDefault();
alert("add new list");
},
Start: function(){
//note: my directory structure is localhost/playground/todo/
Backbone.history.start({pushState: true, root: "/playground/todo/"});
},
initalize: function(){
},
index: function(){
var todoListView = new TodoListView({ collection: TodoItemCollection });
}
}));
Here is how I call my route:
$(function() {
TodoRouter.Start();
});
And lastly here is how I call a link:
New List
The problem that I am running into is that when I call the link, the page stays the same, no alert and the browser displays:
http://localhost/playground/todo/#list/add
Now here is the funny part, if I refresh the page, the url become:
http://localhost/playground/todo/list/add
and I get the alert. So I have a feeling I am missing a key point somewhere. Any help would be greatly appreciated!
You have pushState: true and that's why it preferred the slash / instead of hash #
Either change that or remove the hash
You're trying to use Backbone Routes and html5 pushState.
As the backbone documentation said:
"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."
So if you want to trigger some functions through uri (localhost/webapp/#about) you just need to use Backbone Routes.
If you want to use Backbone Routes and pushState, you'll need a back-end to answer requests made to your readable url (localhost/webapp/about) and need to use backbone.navigate method to avoid the browser understand <a href="#someRoute"> as a html anchor.
here you can see a complete example
You can't trigger your route, because when pushState:true the href="#route" has the same behavior as a html anchor.

Resources