What is causing only one route to be functional with the Backbone Router?

In the segment below, I have incorporated a variety of routes and view events created using Backbone.js. All the routes seem to be working as expected except for the last one 'products'.

Initially, I had it set up differently but noticed that it wasn't triggering, and even after modifying it to match the other views and routes, it still doesn't function properly.

Does anyone have any insights into why this might be happening? Any help would be greatly appreciated!

I'm relatively new to Backbone. Thanks in advance! Rich

<!doctype html>
    <html>
        <head>
            <title>Undie Couture by Lauren Copeland</title>
            <link type="text/css" rel="stylesheet" href="/assets/css/museosans_500_macroman/stylesheet.css" />
            <link type="text/css" rel="stylesheet" href="/assets/css/bootstrap/bootstrap.css" />
            <link type="text/css" rel="stylesheet" href="/assets/css/site/front-styles.css" />
        </head>
        <body>
            <div id="wrapper">
                <div class="content">
                    <header>
                        <div class="container">
                            <div id="logo"></div>
                            <nav>
                                <ul>
                                    <li><a href="/#/products/">shop</a></li>
                                    <li><a href="/#/contact">contact</a></li>
                                    <li><a href="/#/about">about</a></li>
                                    <li><a href="/#/wholesale">wholesale</a></li>
                                </nav>
                            </nav>
                        </div>
                    </header>
                    <div class="container">
                        <div class="page" id="first" style="display:none;"></div>
                        <div class="page"></div>
                    </div>
                </div>
            </div>
        </body>
        <script type="text/template" id="title-temp">
            <%= title %>
        </script>
        <script type="text/template" id="logo-temp">
            <a href="/#/home" id="logo"><img src="/assets/img/logo-strip.png" /></a>
        </script>
        <script type="text/template" id="home-temp">
            <%= title %>
        </script>
        <script type="text/template" id="page-temp">
            <h1><%= page.pluck('title') %></h1>
            <div id="body">
               <%= page.pluck('body') %>
            </div>
        </script>
        <script type="text/template" id="product-temp">
            <h1><%= page.pluck('name') %></h1>
            <div id="body">
                <%= page.pluck('description') %>
            </div>
        </script>

        <script type="text/javascript" src="/assets/js/libs/jquery/jquery.js">    </script>
        <script type="text/javascript" src="/assets/js/libs/underscore/underscore.js"></script>
        <script type="text/javascript" src="/assets/js/libs/backbone/backbone-min.js"></script>
        <script type="text/javascript" src="/assets/js/libs/bootstrap/bootstrap.js"></script>


<script type = "text/javascript">

var Router = Backbone.Router.extend({
    routes: {
        "home": "home",
        "about": "about",
        "contact": "contact",
        "wholesale": "wholesale",
        "products": "products"
    }
});
var Page = Backbone.Model.extend({
    initialize: function() {
        console.log('Page model loaded');
    },
    defaults: {
        "id": "",
        "title": "",
        "body": "",
        "slug": ""
    },
    urlRoot: '/backbone/page'
});

var Pages = Backbone.Collection.extend({
    initialize: function() {
        console.log('pages collection loaded');
    },
    url: '/backbone/page'
});


var HomeView = Backbone.View.extend({
    template: $('#standard').html(),
    el: '.page:first',
    change: function() {
        $('.page').fadeOut('slow');
    },
    render: function() {
        logo = $('#logo-temp').html();
        $('#logo').html(logo);
        $('.content').attr('id', 'home');
        var that = this;
        that.change();
        compiled = _.template($('#home-temp').html(), {
            title: ''
        });
        that.$el.html(compiled);
    }
});

var AboutView = Backbone.View.extend({
    template: $('#standard').html(),
    el: '.page:first',
    change: function() {},
    render: function() {
        console.log('render')
        var that = this;
        logo = $('#logo-temp').html();
        $('#logo').html(logo);
        $('.content').attr('id', 'about');
        aboutPage = new Pages();
        aboutPage.fetch({
            data: {
                id: 3
            },
            success: function() {
                $('#first').fadeOut({
                    duration: 400,
                    complete: function() {
                        console.log(aboutPage.models)
                        compiled = _.template($('#page-temp').html(), {
                            page: aboutPage
                        });
                        that.$el.html(compiled).delay(300).fadeIn();
                    }
                });
            }
        });
    }
});

var ContactView = Backbone.View.extend({
    template: $('#standard').html(),
    el: '.page:first',
    change: function() {
        $('.page').fadeOut();
    },
    render: function() {
        var that = this;
        logo = $('#logo-temp').html();
        $('#logo').html(logo);
        $('.content').attr('id', 'contact');
        contactPage = new Pages();
        contactPage.fetch({
            data: {
                id: 2
            },
            success: function() {
                $('#first').fadeOut({
                    duration: 400,
                    complete: function() {
                        console.log(contactPage.models)
                        compiled = _.template($('#page-temp').html(), {
                            page: contactPage
                        });
                        that.$el.html(compiled).delay(300).fadeIn();
                    }
                });
            }
        });
    }
});

var WholesaleView = Backbone.View.extend({
    template: $('#standard').html(),
    el: '.page:first',
    change: function() {},
    render: function() {
        console.log('render')
        var that = this;
        logo = $('#logo-temp').html();
        $('#logo').html(logo);
        $('.content').attr('id', 'about');
        wholePage = new Pages();
        wholePage.fetch({
            data: {
                id: 4
            },
            success: function() {
                $('#first').fadeOut({
                    duration: 400,
                    complete: function() {
                        console.log(wholePage.models)
                        compiled = _.template($('#page-temp').html(), {
                            page: wholePage
                        });
                        that.$el.html(compiled).delay(300).fadeIn();
                    }
                });
            }
        });
    }
});

var ProductView = Backbone.View.extend({
    template: $('#standard').html(),
    el: '.page:first',
    change: function() {},
    render: function() {
        var that = this;
        logo = $('#logo-temp').html();
        $('#logo').html(logo);
        $('.content').attr('id', 'about');
        wholePage = new Pages();
        wholePage.fetch({
            data: {
                id: 4
            },
            success: function() {
                $('#first').fadeOut({
                    duration: 400,
                    complete: function() {
                        console.log(wholePage.models)
                        compiled = _.template($('#page-temp').html(), {
                            page: wholePage
                        });
                        that.$el.html(compiled).delay(300).fadeIn();
                    }
                });
            }
        });
    }
});

var router = new Router();

var page_model = new Page();

var page_col = new Pages();

var home = new HomeView();

var about = new AboutView();

var contact = new ContactView();

var wholesale = new WholesaleView();

var products = new ProductView();

router.on('route:home', function() {
    home.render();
});

router.on('route:about', function() {
    about.render();
});

router.on('route:contact', function() {
    contact.render();
});

router.on('route:wholesale', function() {
    wholesale.render();
});

router.on('route:products', function() {
    products.render();
});


Backbone.history.start(); 
</script>

Answer №1

Your hyperlink for shops contains an unnecessary trailing slash

<a href="/#/products/">

Instead, it should be

<a href="/#/products">

I was able to get the route working by removing that extra slash.

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

How do I add a "Switch to Desktop Site" link on a mobile site that redirects to the desktop version without redirecting back to the mobile version once it loads?

After creating a custom mobile skin for a website, I faced an issue with looping back to the mobile version when trying to add a "view desktop version" link. The code snippet below detects the screen size and redirects accordingly: <script type="text/j ...

Is there a way for senders to also view their own messages using socket.io?

Using socket.io, I am trying to send a message to a specific user based on their socket.id, and also ensure that the sender can see their own message. The code snippet I am using for this is: socket.to(msg.id).emit('chat message', msg.message);, ...

Initiating a Page with Dynamic Modal Window according to Backend Exigencies

Dear experts, can you help me with a problem? I want to implement a modal window on an vb.aspx page if a specific condition from the codebehind query is true. How can I achieve this? Thank you! ...

Storing a variable in jQuery using AJAX and retrieving it at a later time

Hey there! I'm currently using jQuery and AJAX to fetch the user ID of the logged-in user. I'm storing this information in a variable so that I can use it for some logic later on. However, I'm facing issues with accessing it. Here's my ...

Resolve Redux-Firestore issue #45: Possible Solutions?

I'm facing an issue where deleting a document from Firestore results in my Redux store showing it as null instead of removing it. Even though the document is deleted in Firestore, this inconsistency causes frontend issues because my .map functions can ...

Utilizing a PHP-scripted multidimensional array in an AJAX success function

I've encountered an issue while trying to access a multidimensional array passed to my AJAX success function. Here's how I'm sending the data: $response = array(); $response['message']['name'] = $name; $response['m ...

Determining the Testing Status of a Node Package Management (NPM) Package

As someone who is new to Node.js and NPM, I have a question that may seem naive. Is there a method to determine if a package published on NPM has been tested? And if so, can this process be automated? Are there any tools or frameworks available that can va ...

A Step-by-Step Guide to Clearing JSON Cache

I'm currently utilizing jQuery to read a JSON file. However, I've encountered an issue where the old values are still being retrieved by the .get() function even after updating the file. As I continuously write and read from this file every secon ...

Updating the state of a React Component using data from 2 input fields

I am facing an issue with two input fields of type "file" in a React component. My goal is to load a JSON file into each input field, save the data from both files into separate variables, and update the state of the component. However, I have noticed that ...

Trouble with uploading images to folder using PHP and AJAX is causing confusion

I'm having trouble uploading a photo using ajax and php. Despite following advice from other sources, I can't seem to make it work. Is there anything in my code that appears to be incorrect? The ajax request seems to go through successfully, so ...

Encountering a JS error that states: "Uncaught SyntaxError: missing ) after argument list" when running the following code

I keep encountering the error message: "Uncaught SyntaxError: missing ) after argument list" when I try to call the delete function within the createHtmlview function. console.log("Delete Item is being called") } ...

The function call FirstName.Val() is invalid and will not execute as intended

When attempting to create an array called requestData using the input values from a user details form, I encountered an error stating that .val() is not a function. The code snippet where this problem occurred is as follows: Jquery $('#submit' ...

Using a function with a parameter as an argument in an event handler

Imagine you have the code snippet below: $('#from').focus(listExpand(1)); $('#to').focus(listExpand(3)); I am facing an issue as the code is not behaving as expected. I believe the problem lies in passing a function result instead of ...

Utilizing Jquery to Pass an Optional Function to Another Function

I am currently working on a function that utilizes AJAX to submit data and then displays a dialog indicating whether the process was successful or not. Everything seems to be functioning smoothly, but I now want to add the capability of passing an addition ...

Managing email delivery and responses within Nextjs server functions using Nodemailer and React Email package

Currently, I'm working on a Next.js project that involves sending emails. The functionality works as expected, but I've encountered an issue when trying to verify if the email was successfully sent or not. Here's my current setup: await tran ...

Steps to show the chosen index value in an alert pop-up using Ionic 2 framework

I'm in the process of trying to showcase a selected index value within an Ionic 2 alert box. However, I'm struggling to find the correct method to display it in the Ionic prompt. This pertains to the home.ts import { Component } from '@ang ...

Error encountered when attempting to retrieve posts using Axios: Unexpected symbol detected, expected a comma (25:4)

I've been working on implementing an axios getPosts function, but I keep encountering a syntax error that I can't seem to locate in my code. getPosts = async () => { let data = await api.get('/').then(({ data }) => data); ...

Why doesn't the element I'm clicking return as expected?

Below is the code provided <!DOCTYPE html> <html> <body> <p id="demo">Demo Element</p> <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script> $('#demo' ...

Strategies for integrating user data into Vue components within Laravel

I've successfully logged my user data in the console, but when I try to display the data on Contalist page, nothing is returned. I'm new to using Vue and need help implementing it into my projects. Below are my PHP controller and Vue component fi ...

Locating the Active Object's Coordinates in fabric.js

When using fabric js, I have successfully drawn various shapes like circles and rectangles. However, I encountered an issue while trying to obtain the coordinates of the rectangle using the fabric.rect() method. canvas.getActiveObject().get('points& ...