Making an Ajax request to the GitHub API using Backbone

Currently delving into Backbone, I have encountered an issue while attempting to fetch JSON data from the GitHub API. My goal is to create a simple web application that retrieves a paginated list of open issues from the GitHub Rails repository. The stumbling block for me lies in fetching the JSON data containing the initial page of issues without concern for pagination.

While I can easily attain the first page of issues using a straightforward jQuery Ajax call, this method does not fully utilize Backbone.

$.getJSON("https://api.github.com/repos/rails/rails/issues?state=open", function(data) {...});

Below is my Backbone JavaScript code -- currently keeping everything within one file for simplicity.

$(function() {
    // Namespace defined as RailsIssues
    window.RailsIssues = {
        Models: {},
        Collections: {},
        Views: {}
    };

    // Global function simplifying template property referencing
         window.template = function(id){
        return _.template( $('#' + id).html());
    }

    // Issue model
    RailsIssues.Models.Issue = Backbone.Model.extend({
    });

    // IssueList collection
    RailsIssues.Collections.Issues = Backbone.Collection.extend({
        model: RailsIssues.Models.Issue,

        /* No data returned */
        url: "https://api.github.com/repos/rails/rails/issues",

        parse: function(data){
            return data;
        }
    });

    // Issue view
    RailsIssues.Views.Issue = Backbone.View.extend({
        tagname: 'li',
        template: template('issueTemplate'),
        initialize: function(){
            this.render();
        },
        render: function(){
            this.$el.html(this.template(this.model.toJSON()));
            return this;
        }
    });

    // Issues view
    RailsIssues.Views.Issues = Backbone.View.extend({
        template: template('issuesTemplate'),
        render: function(eventName) {
            _.each(this.model, function(issue) {
                var number = issue.attributes['number'];
             
               var xtemplate = this.template(issue.toJSON());
                $(this.el).append(xtemplate);
      
              }, this);

            return this;
        }
    });

    Backbone.sync = function(method, model) {
        alert(method + ": " + model.url);
    }

    var issuesView = new RailsIssues.Views.Issues({ collection: RailsIssues.Views.Issues });
    $(document.body).append(issuesView.render().el);
});

Below is the HTML code -- primarily showcasing resources with no significant content at the moment. Initially focused on retrieving and displaying the data in the document body before moving onto styling.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script id="issuesTemplate" type="text/template">
          <li><%= number %></li>
      </script>
      <script src="script/underscore.js"></script>
      <script src="script/jquery-1.11.1.min.js"></script>
      <script src="script/backbone.js"></script>
          <script src="script/script.js"></script>
        <title>Rails Issues</title>
    </head>
    <body>
        <header>
            Open Issues in Rails GitHub
        </header>
    </body>
</html>

I am encountering difficulties in retrieving the JSON data. The current code fails silently, providing no output, whereas the jQuery method mentioned earlier successfully returns the desired JSON data. Any guidance or suggestions would be greatly appreciated.

Answer №1

Despite struggling to identify the error in my code, I finally managed to fix it with the assistance of an informative tutorial. Thanks to this resource, I was able to successfully execute the Ajax API call.

The code is designed to retrieve the first page of results (specifically, issues posted on the Rails GitHub page) and showcase the issue numbers in an unordered list format.

Below is the HTML containing the templates and script tags arranged in the correct sequence:

<!DOCTYPE html lang="en">
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="icon" type="image/x-icon" href="https://assets-cdn.github.com/favicon.ico">      

         <script id="issuesTemplate" type="text/template">
            <% $.each(issues, function() { %>
                <li><%= this.number %></li>
            <% }); %>          
        </script>       

        <script src="script/underscore.js"></script>
        <script src="script/jquery-1.11.1.min.js"></script>
        <script src="script/backbone.js"></script>
        <script src="script/script.js"></script>
        <title>Rails Issues</title>   
    </head>
    <body>
        <header>
            Open Issues in Rails GitHub
        </header>
        <div class="theList">Loading... Please Wait.</div>
    </body>
</html>  

Additionally, here is the script.js file:

var RI = {
    run: function() {
        this.listview = new this.listView();
        this.issuescollection = new RI.issuesCollection();
        this.router = new this.Router();
        Backbone.history.start();
        this.router.navigate('list_issues');
    }
};

RI.Router = Backbone.Router.extend({
    routes: {
        'list_issues': 'renderListIssuesPage'
    },

    renderListIssuesPage: function () {
        RI.listview.setElement('div.theList');
        RI.listview.listIssuesPage();
    }
});

RI.issueModel = Backbone.Model.extend({

});

RI.issuesCollection = Backbone.Collection.extend({
    model: RI.issueModel,   
    url: 'https://api.github.com/repos/rails/rails/issues'
}); 

RI.listView = Backbone.View.extend({
    el: 'div.theList',

    template: _.template($('#issuesTemplate').html()),

    initialize: function () {
        _.bindAll(this, 'listIssuesPage', 'render');
    },

    render: function (response) {
        var self = this;

        this.$el.html(this.template({issues: response}));
    },

    listIssuesPage: function (querystring) {
        var self = this;

        RI.issuescollection.fetch({
            data: querystring,
            success: function(collection, response) {
                self.render(response);
            }
        });
    }
});

$(function() {
    RI.run();
}); 

Answer №2

Upon attempting to execute your code, I encountered an issue with the absence of the issuesTemplate and issueTemplate. It came to my attention that although you instantiated the view, you neglected to do so for the collection or model. Instead, you linked the view's collection to the definition of the views issues (but not the actual collection instance):

var issuesView = new RailsIssues.Views.Issues({ collection: RailsIssues.Views.Issues });

Additionally, after creating the new collection, you must call fetch() in order for Backbone to execute the API call required to retrieve the necessary JSON data. My suggestion would be to implement something similar to the following:

var issuesCollection = new RailsIssues.Collections.Issues();
issuesCollection.fetch();  // initiates the GET request
var issuesView = new RailsIssues.Views.Issues({ collection: issuesCollection });

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

Pagination does not refresh when conducting a search

HTML Code: The following HTML code displays a table with a search filter. . . <input type="search" ng-model="search.$" /> <table class="table table-striped table-bordered"> <thead> <tr> <th><a href ...

changing a class to JSON format with nlohmann library in C++

Attempting to convert my class to json in visual studio 2017 is resulting in linker errors C2440. Severity Code Description Project File Line Suppression State Error C2440 'initializing': cannot convert from 'ns::person&apos ...

What could be the reason behind receiving an "undefined" message when attempting to access db.collection in the provided code snippet?

var express = require('express'); var GoogleUrl = require('google-url'); var favicon = require('serve-favicon'); var mongo = require('mongodb').MongoClient; var app = express(); var db; var googleUrl = new GoogleUrl( ...

Potential Scope Problem in Angular JS Controller

The HTML code snippet I have is as follows: <body ng-controller = "Control as control"> <button ng-click = "control.prepareAction()">Do Action </button> <div id="one" ng-hide = "!control.showOne" > <div> <h6> ...

What is the best location for storing your front-end javascript files?

As I work on my web application using Express, I've utilized the application skeleton provided by the express-generator. To bundle my client-side assets, I've integrated webpack into my project. Now, I'm faced with a dilemma on where to st ...

What possible problem could cause the error: an invalid character '-' appearing after a top-level value?

Utilizing the go-jmespath Golang library for querying a json file, my code is structured as follows: package main import ( "encoding/json" "log" "github.com/jmespath/go-jmespath" ) func main() { var jsonBlob = []byte(`[ { "o ...

Utilizing Input Data from One Component in Another - Angular4

As a newcomer to Angular, I'm facing an issue where I need to access a variable from ComponentA in ComponentB. Here's the code snippet that demonstrates what I'm trying to achieve (I want to utilize the "favoriteSeason" input result in the " ...

Leveraging the .reduce method to perform specific calculations and generate an object containing the results of an array

After successfully achieving the desired results, here is the data manipulation I have done: const days = [ { date: '2016-12-13T00:00:00.000Z', stats: [ { name: 'Soft Drinks', sold: 34, }, { name: 'Snacks&apo ...

The issue with Jquery Ajax is that it fails to properly update MySQL values

Greetings, I am currently attempting to utilize AJAX to update data from a modal form. Although I submit the data successfully, it does not reflect the changes in the database. Here is my JavaScript code: <script> jQuery(document).ready(functio ...

Having trouble getting the Babel plugin transform-remove-console to function properly with the Vue CLI 4 and @vue/cli-plugin-babel/preset?

When you create a VueJS project using Vue CLI 4, Babel is configured with a useful preset in babel.config.js: module.exports = { presets: [ '@vue/cli-plugin-babel/preset', ], }; I am attempting to use babel-plugin-transform-remove-conso ...

Replace the contents of the array index with the data retrieved from the

Having trouble changing the index of array in my Redux state :/ I have a data set coming from an API endpoint with the following results: e.g. data: [{ 0: {id: 123, name: test1 }, 1: {id: 456, name: test2 }, 3: {id: 789, name: test3 }, }] ...

To effectively utilize the expect function in testing, should you use it over softAssertAll or find a way to explicitly display in the test results that the softAssertAll has successfully passed? (Error

Utilizing the 'soft-assert' library (soft-assert library) allows me to implement assertions in my test steps without halting the entire test if any of them fail. While softAssertAll() command works effectively to verify all soft-assert at the en ...

Ways to Press the Enter Key on Different Browsers

Looking for a solution to get the keypress action working properly? I have a chat feature where I need to send messages. Here is what I have in the Form-tag (JSP based): function SendMessage() { if (event.keyCode===13) { ale ...

The command 'create-react-app' is not valid and cannot be recognized as an internal or external command, operable program, or batch file

I've been struggling to set up a React project, as the create-react-app my-app command doesn't seem to be working. Can anyone offer some assistance? Here are the commands I'm using: npm install -g create-react-app create-react-app my-app ...

Angular 9: Trouble encountered with utilizing custom error handler for error instances

I'm currently working on implementing a custom error handling system in Angular using the ErrorHandler class. This is what my Globalerror service looks like: export class CustomErrors extends Error { button?: any; errObject: any; constructor() ...

Purge the cache of CSS and JavaScript in your browser

Is there a way to clear JavaScript and CSS cache in ASP MVC without having to clear browser history? ...

Ensuring that localStorage objects continue to iterate when clear() is called within the same function

When a user completes the game loop or starts a new game, I want to clear all local storage while still keeping certain values intact. Currently, I am able to do this for sound volume values: // code inside a conditional statement triggered when starting ...

"Facing rendering issues with handlebars.js when trying to display an

I have been diligently working on trying to display an array from my running express app, but despite the lack of errors, nothing is being rendered. Take a look at my handlebars template: <h3>Registered Users:</h3> <h5>{{users}}</h5& ...

Is there a way to showcase a row of images when a button is clicked?

I am looking to create a functionality where pressing one of the buttons shown in the image below will toggle visibility of specific sections containing 3 images each. For example, clicking on "Tapas" will only display tapas images and hide main course ima ...

Can you demonstrate how to incorporate a new line within a for loop?

I have an array of letters and I need to display them on the screen using a for loop. My goal is to make every sixth letter appear on a new line. Here is the code snippet: https://i.stack.imgur.com/lHFqq.jpg <script> export default { data() { ...