Update the default GET URL structure in Backbone

REST API:

If you need to access my REST API, you can do so by sending a GET request to the following URL:

http://localhost/Project/index.php/rest/resource/car

This will retrieve all the data in the table in JSON format. Additionally, you can get specific data for a car with a particular carId using this URL:

http://localhost/Project/index.php/rest/resource/car/carId/2
.

Backbone Integration:

    this.mycar.fetch({
        data: { 'carId': '1' },
        success: function () { ...

When using the above code in Backbone, it generates a request like

http://localhost:7070/Project/index.php/rest/resource/car?carId=1
, which doesn't match my REST API requirements. How can I modify it?

Edit: Model and Collection Setup:

var Car = Backbone.Model.extend({
    urlRoot: ROOT + 'car',
    idAttribute: 'carId'

});

var Cars = Backbone.Collection.extend({
    model: Car,
    url: ROOT + 'car',
})

Answer №1

Within jQuery's $.ajax, the data property is utilized for query parameters in GET requests.

It is recommended to update the model's .url method/property instead of setting it directly as a parameter:

this.mycar.url = 'api_entry_point/car/' + car_id;

edit: Noting the presence of carId in your url resource, which may be unconventional. This could potentially lead to using idAttribute: 'carId' in the model constructor. Consider revising the url structure:

http://localhost/Project/index.php/rest/resource/car/2

In the revised url above, the 2 represents the model id without the need for carId. It might be more logical to use cars instead of car segment since resources usually contain multiple items:

http://localhost/Project/index.php/rest/resource/cars/2

Ultimately, this is merely a matter of personal preference.

Answer №2

To define the URL in your backbone model file, you can use the following format:

url: "http://localhost/Project/index.php/rest/resource/car/carId/" + this.id

When setting the ID in your backbone view, you can do it like this:

this.model.id = this.id;
this.mycar.fetch({
    success: function() {

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 can I align the text in a dropdown menu to the center on Safari?

How can I center the option text in a drop down menu using CSS? display: block; height: 60px; margin-left: 10%; margin-right: 10%; margin-top: 10px; min-height: 60px; text-align: center; The option text is centered in Firefox browser, but not in Safari. ...

Vue Js and form-data Event: A deeper look into handling form

Hey there #Vue.js2 I'm facing an issue while trying to create a function that is called within a form submit event. This particular function needs both the EVENT and ID as parameters. The problem arises when I call this function, as I am unable to spe ...

Troubden array filtration in Angular is malfunctioning

I recently developed an angular "filter component" intended to filter an array and display its contents. The keyword used to filter the array, value, is obtained from another component through a service. While the HTML displays both the value and the entir ...

The functionality of nth-child(odd) seems to be malfunctioning when there is a change

When I group certain elements together in one container, I like to make odd elements stand out by giving them a different background color. However, when I try to filter elements based on conditions and move unwanted elements to another class, the nth-chil ...

Angular - Issue with deleting data using HTTPClientModule

I've encountered a strange issue with my Angular app where the delete request is not functioning as expected without any visible errors. Here's the code snippet from my service: import { Injectable } from '@angular/core'; import { HttpC ...

Operate on Nested JSON Arrays

I have the following JSON array: var salesData = [ { "products": "Cars", "solddate" : "2022-01-01", "noofitems" : " ...

What is the proper method for implementing an event listener exclusively for a left mouse click?

Is there a way to make Phaser recognize only left mouse clicks as click events, excluding right and middle clicks? Check out this Phaser example at the following link: https://phaser.io/examples/v2/basics/02-click-on-an-image. ...

Retrieve the JSON data once the user has applied the filter

Is there a more efficient way to improve the performance of handling an extremely large JSON file with 5000 keys and 5 values each? Currently, I am using AngularJS with a backend in Drupal 7. Here is my view: <ul class="list-group border-0"> < ...

Issue with click event not triggering initial interaction with dynamically inserted elements through AJAX requests

I am experiencing an issue with my ul element that is being populated with li elements through AJAX. When I try to click on these li elements, something peculiar happens. The functionality only works when I click for the second time, not the first. Despit ...

Tips for implementing JsonSerializer.Deserialize<T> with application code trimming enabled

I've been trying to understand how to use JsonSerializer.Deserialize<T> with application code trimming enabled. The compiler is indicating that I need to use an overload that requires a JsonTypeInfo or JsonSerializerContext. I've searched t ...

JavaScript is displaying an error message stating that the array variable has not been

I'm currently going through a Javascript tutorial on YouTube and I've encountered an error that's puzzling me. Everything seems to be in order with the array definition. Uncaught ReferenceError: words is not defined window.addEventListe ...

Error occurred while trying to call android.app.Service() without any arguments provided

In my experience with using Retrofit to fetch data from an API, I encountered a peculiar issue. The response would fail only when the service Array contained data; if it was null, the response would be successful and all other data would be present. Howeve ...

What is the best way to retrieve a variable within an AngularJS controller?

I am working with a binding parameter eid: '@' Inside my constructor, you will find the following method: this.$onInit = () => { console.log('eid: ', this.eid) } console.log("eid in another section: ", this.eid) Upon running t ...

"Why does the useEffect in Next.js fire each time I navigate to a new route

Currently, I have implemented a useEffect function within the Layout component. This function is responsible for fetching my userData and storing it in the redux-store. However, I have noticed that this function gets triggered every time there is a route c ...

Experiencing trouble with calculating the total value of an array of objects due to NaN errors

I've been working on developing this web application in VUE.js, but I'm facing an issue with a specific function where I am attempting to sum the values of each object within an array. This is resulting in a NaN (Not a Number) error. Let's t ...

Retrieving cascading values in a dropdownlist using Asp .NET MVC

In my current project, I am using Asp.Net MVC4. In a form, I have implemented cascading dropdown lists. When I select "Distrito" in one dropdown list, it correctly loads the corresponding values for "Sucursal". The insertion of records works fine. However, ...

Obtain a byte array from an AngularJs controller

In my Angular controller, I am working with a byte array. When the download button is clicked in the view, I want to trigger the browser's download/saveAs dialog with 'report.pdf' as the pre-populated filename and PDF as the file type. After ...

Troubleshooting problem with hiding options in Jquery Chosen

Utilizing the chosen multiple selection feature, I have encountered an issue where selected options remain visible after being hidden and updated. Here is the code snippet: $("#ddlcodes").find("option[value ='" + codeValue + "']").hide(); $("#d ...

Parse multiple JSON files, manipulate their contents, and store the updated data

I'm currently working on implementing this functionality using Gulp. Locate and access all files with the extension .json within a designated directory, including any subdirectories. Perform modifications to the files in some manner, such as adding ...

Is there a way to customize the color of the bar displaying my poll results?

My poll features two results bars that are currently both blue. I attempted to change the color of these bars but was unsuccessful. I've searched for solutions on stack overflow, specifically How can I change the color of a progress bar using javascr ...