Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

backbone go to another page

Tags:

backbone.js

This is likely a super easy question, in a view, I tie a 'click' event to a button, when that event is clicked I wish to redirect to a totally new page (e.g not a route).

e.g.

events : {      
    "click .button" : 'redirect'
},
redirect : {
     window.location('otherlink');
}

I could use window.location, but seems thats the wrong way? any ideas appreciated?

like image 988
Xrender Avatar asked Feb 20 '12 15:02

Xrender


People also ask

How to add events to the view in backbone?

That's the data which is injected into the template. It's time to add some events to the view. In Backbone.js the event's definition is a just a hash. You firstly type the name of the event and then a selector. The values of the properties are actually methods of the view.

What is backbone Todo?

Backbone.js is a JavaScript framework for building flexible web applications. It comes with Models, Collections, Views, Events, Router and a few other great features. In this article we will develop a simple ToDo application which supports adding, editing, and removing tasks.

What is the best backbone framework for single page application?

Backbone.js has everything you need for building a fully functional, single page application. We could even bind it to a REST back-end service and the framework will synchronize the data between your app and the database. The event driven approach encourages modular programming, along with a good architecture.

How to update and save the application as URL in backbone?

The Backbone.js navigate method is used to update and save the application as URL. It can also be done by calling the route function. fragment: It specifies the name of the parameter in which URL will be displayed. options: It specifies the options such as trigger and replace to call the route function and to update the URL. Let's take an example.


1 Answers

Backbone has a 'router' (which is basically what I've come to know as a controller). You can use it to do navigation. For example:

var MyApp = new Backbone.Router();
MyApp.navigate('newPage', {trigger: true}); 

trigger makes sure an event is thrown in case you are using said events.

You can team the router up with Backbone's 'history' and build a pretty awesome one page application very easily.

Edit: Never mind, I just re-read that you didn't want to use a route. My two cents - make a wrapper to do the navigation for you. I just had to refactor a pretty big javascript application a few weeks ago, taking out a hundred or so window.locations so that it could be unit tested. Not so much fun. I.e:

MyApp = {
    navigate: function (url) { window.location = url; }
}

Now you can actually do unit testing, as you can override MyApp.navigate with something that does nothing when called. You can also add in business logic if need be.. or filters.. or anything else.. without needing to disturb the rest of the application. For example, if the url is outside of the site, open it in a new window instead of just showing in the same page.

like image 164
Stephen Avatar answered Dec 25 '22 08:12

Stephen