Discover the secrets of accessing two distinct objects returned by a single REST URL with Backbone

I am working with a REST URL that looks like this:

/users/<user_id>/entities

This URL returns data containing 2 objects as follows:

{
    "players":
    {
       "test_player2":
       {
           "_id": "test_player2",
           "user": "f07590567f3d3570b4f35b4fd79f18b3"
       },
       "test_playerX":
       {
           "_id": "test_player2",
           "user": "f07590567f3d3570b4f35b4fd79f18b3"
       }
    },
    "games":
    {
      "game1" :{},
      "game2" :{},
    }
}

I am figuring out how to structure my Backbone Objects in order to utilize this data effectively.

My goal is to create two separate Backbone objects: Player and Game, both of which should be populated using the same URL mentioned above.

One question that arises is whether it is considered proper practice to design a REST URL in this manner?

Answer №1

Is it considered best practice to structure REST URLs in this way?

No, it is not the correct practice. In REST architecture, each URL should represent a single resource. Therefore, instead of using /users/<user_id>/entities, you should use /users/<user_id>/players to return a list of players and /users/<user_id>/games to return a list of games.

There may be instances where you have limited control over the API response, particularly with nested objects:

{
    "players":
    {
        "id": 1,
        "games":
        {
           "id": 1745,
           "title": "Team Fortress 2"
        }
    }
}

In such cases, you can utilize the model's parse function like this:

parse: function(response)
{
    if(_.isArray(response.games))
    {
        this.games = new GamesCollection(response.games,{parse: true});
    }
}

By customizing the parse function and using {parse:true}, you can extend your models as needed. While it's not the preferred method (as collections should handle their own models), it can be useful for working with complex API responses beyond your control.

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

Problems with accessing Ajax login

I'm encountering issues with an Ajax login function. Despite finding a similar question that didn't help, I'm still unsure of the problem. It's perplexing because this code functions without any problems in another project. Hopefully, ...

Having trouble parsing the BODY of a NodeJs JSON post request?

I've been working on creating a basic API using nodeJS, but I've run into a problem while trying to post data. Below is my app.js file: const express = require('express'); const feedRoutes = require('./routes/feed'); const ...

Issues with routing on Zeit Now platform are causing a 404 NOT FOUND error when attempting to reload pages that are not the

Recently, I purchased a Next.js template from Themeforest and attempted to deploy it to Zeit Now. This particular project is set up as a monorepo using Lerna and Yarn workspace. The code for the Next.js app can be found inside the packages/landing folder. ...

What is the reason behind my button appearing beneath my links in React?

Here is an image showcasing the current header render. The header consists of a HeaderMenu and 3 Links. While the links are functioning properly, the HeaderMenu is causing the links to be positioned below it. The HeaderMenu includes a div that wraps a Butt ...

Angucomplete Alternative solves the challenge of accessing remote URLs

I have been using the Angucomplete Alt directive for creating an autocomplete feature. It has been working well so far, but now I want to customize a specific request to be sent to my server. <div angucomplete-alt id="input-name" ...

Component updates are not working in VueJS

My Vue 1 component requires an object as a prop that needs to be filled by the user. This object has a specific structure with properties and nested inputs. The component is essentially a modal with a table containing necessary inputs. I want to perform v ...

Adding or removing a class using Jquery based on the condition of form validation

I am facing a problem with the following code that adds and removes classes to bring the next stage of the form. The form progresses step by step where certain fields need to be filled before validation on the next button, followed by filling other fields. ...

Tips for initializing a jstree with no content?

When I click a button, I send a key to the controller and retrieve my lists using JSON. The array inside my lists serves as my children in my jstree. $("#btnSearch").on("click", function () { alert("I'm also here"); $.ajax({ ...

Display a component just once in React or React Native by utilizing local storage

I am struggling with a situation where I need to display a screen component only once using local storage. It's really frustrating. App.js ... constructor(props) { super(props); this.state = { isLoading: false, }; } component ...

Why does the view get hit when submitting the form but not with the (Django) AJAX call?

I have a form that is successfully hitting the following URL: url(r'(?P<slug>[a-z0-9-_]+?)-(?P<product_id>[0-9]+)-(?P<hotel>[0-1]+)-(?P<shuttle>[0-1]+)/add/$', views.product_add_to_cart, name="add-to-cart"), This ...

Can iPhone/iOS 6 users using Mobile Safari detect if the browser is in full-screen mode? How about compatibility with Android?

I am curious about detecting whether a user is using the "fullscreen feature" in Safari. I'm not referring to starting from the springboard, but rather the feature introduced in iOS 6. There was a similar query on SO where this code snippet was share ...

Managing both clicking and hovering events on a single element, ensuring that the popup modal remains open as long as it is being hovered over

After successfully implementing click and hover functionality on an element, I was able to position the popup relative to the mouse pointer based on a previous solution. However, I am now facing an issue where I want the popup modal to be fixed in a specif ...

Why am I unable to insert data into the 'sessions' database using connect-mongo?

I am utilizing connect-mongo in conjunction with express-session, and the session data is successfully being stored in Mongo. However, I want to enhance the sessions collection utilized by connect-mongo in order to include my own fields on top of it. I h ...

Cease the continuous scrolling of the display element now

I'm having trouble with my progressbar animation when scrolling. It seems to run multiple times instead of just once. I apologize if there's an error in my code. Could someone please assist me? This is the code I am using: <div class="demo- ...

Utilize AJAX to connect to a remote domain from an extension

I am currently working on creating a Chrome extension that will utilize AJAX to fetch data from a specific webpage and then generate notifications based on the content of that page. The webpage I am targeting is a ticketing system, and my goal is to deter ...

What could be causing the issue where only one of my videos plays when hovered over using UseRef?

I'm currently working on a project where I have a row of thumbnails that are supposed to play a video when hovered over and stop when the mouse moves out of the thumbnail. However, I've encountered an issue where only the last thumbnail plays its ...

Creating an installation package for an Electron application on Windows

Is it possible to create a Mac installer for an Electron app using a Windows PC? I have tried running npm make, but it only generates a Windows installer. ...

Tips for Creating an Animated Progress Bar in a Slider

After implementing jQuery, I managed to create a unique slider on my website. This slider included a thumbnail with captions for each photo. Positioned below the thumbnail was a progress bar fixed in the center between the caption and the image. Users can ...

Is it possible to prevent the late method from running during the execution of Promise.race()?

The following code snippet serves as a simple example. function pause(duration) { return new Promise(function (resolve) { setTimeout(resolve, duration); }).then((e) => { console.log(`Pause for ${duration}ms.`); return dur ...

What is the best way to send form data to MongoDB using React?

I am seeking guidance on how to pass the values of form inputs to my MongoDB database. I am unsure of the process and need assistance. From what I understand, in the post request within my express route where a new Bounty is instantiated, I believe I need ...