Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js and pushState

If I enable pushState in the backbone router, do I need to use return false on all links or does backbone handle this automatically? Is there any samples out there, both the html part and the script part.

like image 842
marcus Avatar asked Feb 17 '12 12:02

marcus


People also ask

Is Backbone JS still used?

Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.

Is Backbone JS frontend or backend?

Front-End MVC frameworks (Backbone, Angular, etc) all rely on a backend service to provide the data that, say Backbone, would then use as its model. You could have an entire MVC pattern on the backend that accepts requests and spits out some JSON for a frontend MVC framework to use.

What is the use of backbone JS?

BackboneJS is a lightweight JavaScript library that allows to develop and structure the client side applications that run in a web browser. It offers MVC framework which abstracts data into models, DOM into views and bind these two using events.

Is backbone a MVC?

Backbone is a JavaScript MVC library and that's a key difference. In the JavaScript MVC context, a framework usually means that you need to configure your code to get things done.


1 Answers

This is the pattern Tim Branyen uses in his boilerplate:

initializeRouter: function () {   Backbone.history.start({ pushState: true });   $(document).on('click', 'a:not([data-bypass])', function (evt) {      var href = $(this).attr('href');     var protocol = this.protocol + '//';      if (href.slice(protocol.length) !== protocol) {       evt.preventDefault();       app.router.navigate(href, true);     }   }); } 

Using that, rather than individually doing preventDefault on links, you let the router handle them by default and make exceptions by having a data-bypass attribute. In my experience it works well as a pattern. I don't know of any great examples around, but trying it out yourself should not be too hard. Backbone's beauty lies in its simplicity ;)

like image 82
ggozad Avatar answered Sep 17 '22 15:09

ggozad