Connection between the view and the router's backbone

In my opinion, it is important to maintain isolation between the router and view in data-driven programming paradigm. They should only communicate with each other through model changes that they both subscribe to.

However, I have noticed that different online tutorials approach this issue in various ways. Some instantiate a view inside the router's initialize method, allowing the router to access the view. Others pass the router to the view so that it can listen for changes on the router's route events.

I personally do not believe either of these approaches is correct as they violate the separation of concerns principle. As a newcomer to Backbone, I would appreciate input from someone more experienced and knowledgeable to confirm my thoughts.

Answer №1

Utilizing the Router to instantiate Views is a seamless process that aligns with my typical workflow. When a new URL is detected, the Router efficiently manages the detachment of any unused Views and instantiates the required ones.

Although some may argue that passing the Router reference to the View can be cumbersome, I find it necessary for Views to communicate with the Router in order to facilitate navigation changes. By making the Router globally accessible throughout my App, I can easily utilize App.router.navigate() from any View without the need to pass the reference explicitly.

This strategy makes sense considering only one Router instance is permitted within a Backbone application, simplifying the overall development process.

Answer №2

One technique I've utilized in certain projects involves implementing a global event dispatcher to relay routing events to the views.

window.Dispatcher = _.extend({}, Backbone.Events);

Within the router, you can listen for the 'all' event to capture all route:* events:

initialize: function() {
    this.bind('all', this.onRoute);
},
onRoute: function(route) {
      Dispatcher.trigger(route);
}

For the views, you can bind to Dispatcher events and respond to changes in routes:

initialize: function() {
    Dispatcher.on('route:index', this.onIndex, this)
}

This approach is akin to having a model that updates with route changes, but it feels more streamlined and eliminates the need to pass the router to views. It may not be the conventional way of doing things, but with Backbone, there are numerous approaches at your disposal.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I'm having trouble with the calculator, unable to identify the issue (Typescript)

I'm struggling with programming a calculator for my class. I followed the instructions from our lesson, but it's not functioning properly and I can't pinpoint the issue. I just need a hint on where the problem might be located. The calculat ...

Add a new item to an array in Angular 2 when a click event occurs

I'm trying to add a new list item (which comes from an API) when a button is pressed, but I'm not sure how to do it. Can anyone provide some guidance? Here's the code: <ul> <li *ngFor="let joke of jokes">{{joke.value}}</li> ...

Creating a new row with a dropdown list upon clicking a button

I want to include a Textbox and dropdown list in a new row every time I click a button. However, I seem to be having trouble with this process. Can someone assist me in solving this issue? Thank you in advance. HTML <table> <tr> ...

Encountering issues in parsing JSON for PhoneGap Application

I've been struggling with parsing JSON data for a unique PhoneGap application that is dynamically generated by a localStorage variable. The PHP script is functioning properly, but the JavaScript code seems to be encountering issues when trying to pars ...

What specific files from the Kendo core are required for utilizing Mobile and Angular functionalities?

After browsing through similar questions, I couldn't find a solution. Currently, I am experimenting with Kendo (open source core for now) in a Visual Studio Cordova project. Initially, disregarding Cordova, I am focusing on setting up a basic view wit ...

Populating a clickable list and form with a complex JavaScript object

I have a code snippet that takes an unstructured String and parses it into a JavaScript object. My next step is to integrate it into a web form. You can check out the demo here. The demo displays the structured object hierarchy and showcases an example of ...

I am looking to record my data by utilizing getInitialProps() in next.js

Being a beginner in next.js and react, I am eager to retrieve data from this particular free API link: The component index.js is as follows: import UserList from "./userList"; export default function Home() { return ( <div> &l ...

Transferring a JavaScript Value to a PHP Variable (Under Certain Constraints)

Although I have reviewed existing questions and answers related to this issue, I found that the solutions provided do not suit my specific situation. Discussing the differences between client-side JavaScript and server-side PHP, two common solutions are: ...

When using Material UI TextField with input type "date", the event.target.value does not seem to be accessible

I am currently working with Material UI and React Grid from DevExtreme to build a table that includes an input field of type date. However, when I attempt to enter the date, it does not register the change in value until I reach the year field, at which po ...

Dividing a JSON array by a specific key using lodash underscore

I'm on a quest to extract the distinct items in every column of a JSON array. Here is what I aim to convert : var items = [ {"name": "Type 1","id": 13}, {"name": "Type 2","id": 14}, {"name": "Type 3","id": 14}, {"name": "Type 3","id": 13}, {"na ...

There was a hiccup in the camera positioning as the tweening began

I've been working on a basic program that involves tweening the camera to a specific position. The functionality works smoothly for the most part, however, I encounter some glitches at the start of the transition when trying to pan the camera using or ...

Autofill with JavaScript - Dynamic Form Population

Looking to create a form-based HTML page. The objective is to have the second input box automatically populate based on the user's input in the first input box. I've gone through various guides and tried to implement this script, but it doesn&ap ...

I encountered an issue while attempting to utilize a JavaScript script - I received an error message stating "Uncaught ReferenceError: jQuery is not defined."

Currently, I am utilizing a template with multiple pages and I need to use one of them. I followed all the necessary steps correctly, but I encountered an error in the console that says Uncaught ReferenceError: jQuery is not defined. Below is my HTML scrip ...

Best way to position camera to look at or above a specific location on a sphere in Three.js

Is there anyone out there with a suggestion on how to position the camera so it is directly facing upwards towards a specific point on the sphere? LATEST DEVELOPMENT It turns out that Radio's solution was spot on. Upon further investigation, I disco ...

What are the steps for implementing custom edit components in material-react-table?

I am currently using the official material-react-table documentation to implement a CRUD table. You can find more information at this link: . However, I encountered an issue while trying to utilize my own custom modal components for the "create new" featur ...

How to access and utilize parent directive methods effectively in AngularJS

Looking for advice on the best way to utilize parent directive methods in its child directive. Option one: Pass it as a scope parameter in the HTML where you initialize the child directive. Option two: Make the parent directive required and gain ...

Using regex in Javascript to find and match an ID within a string

JavaScript: var data='<div id="hai">this is div</div>'; I am looking to retrieve only the ID "hai" using a regular expression in JavaScript. The expected output should be, var id = regularexpression(data); The variable id should n ...

The ajv-based middy validator does not adhere to the specified date and time format

When it comes to validation, I rely on middy as my go-to package, which is powered by ajv. Below is an example of how I set up the JSON schema: serviceDate: { type: 'string', format: 'date-time' }, The structure o ...

Node.js: Attempting to arrange ISO dates based on their time span

I am currently working on creating a line chart using the chart.js library. In order to do this, I have an array that contains ISO dates and my goal is to determine which ISO dates belong to the same hour and day. This information will then be used to gene ...

What could be causing a successful return from JQuery Ajax but still showing an error in Firebug within a PHP application?

I'm encountering an issue with my form submission and jQuery validator plugin. Everything seems to be working fine until I reach the submit handler in my jQuery code: submitHandler: function (form) { $.ajax({ type: "POST", url: ...