Display JSON information on a webpage with the help of Backbone framework

At an event in my view, when a key is pressed and released, it triggers a fetch request to the TMDB API.

class Movieseat.Views.Moviesearch extends Backbone.View

  template: JST['movieseats/moviesearch']
  el: '#moviesearch'

  initialize: (opts) ->
    @collection.on('reset', @render, this)
    {@collection} = opts
    @render()
    return

  render: ->
    $(@el).html(@template(collection: @collection))
    return

  events:
    "keyup input": "doSearch"

  doSearch: (e) ->
    @collection.setQuery $(e.currentTarget).val()
    @collection.fetch()
    view = new Movieseat.Views.Movie()
    $('#movies').append(view.render().el)

This represents my collection:

class Movieseat.Collections.Moviesearch extends Backbone.Collection

  url: -> "http://api.themoviedb.org/3/search/movie?api_key=a8f7039633f2065942cd8a28d7cadad4&query=#{@query}"

  setQuery: (q) ->
    @query = q
    return

For example, if the user inputs inception, this is the corresponding fetch request:

http://api.themoviedb.org/3/search/movie?api_key=a8f7039633f2065942cd8a28d7cadad4&query=inception

In my view, I append a template called movies. The goal is to display all the original_title data retrieved from the fetch request within that template and keep it updated as needed.

Answer №1

It seems like your collection is storing elements that are related to movies. Once you have fetched the collection, make sure the models include the properties original_title and poster_path. You can then utilize the pluck method to access these values. Here's an example of how this can be done in JavaScript:

collection.on("sync", function() {
    myView.displayTitles(collection.pluck("original_title"));
})

Correction

After reviewing the code update you shared, I'm curious as to why you're not utilizing these properties within a template for the collection. Since the models in your collection do not specify their type, you cannot use the syntax @model.get('prop'), as @model is not recognized as a Backbone model. Instead, consider using

@model.prop

as the correct syntax.

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

Showing JSX/HTML content depending on the props received

Can you identify the name of this type of expression and do you know in what scenarios it should be applied? {props.type === "big" && <h2>{props.title}</h2>} ...

I'm looking to efficiently convert JSON or GeoJSON data into a Backbone model and then seamlessly transition that model into a Leaflet layer. Can anyone provide guidance on

As I work on refining layer definitions that can be added individually to a collection, my goal is to smoothly render the view or add them to a L.LayerGroup using the leaflet api. However, being new to JavaScript, I am uncertain about how to map the proper ...

Is there a more efficient method to achieve the desired effect without making multiple calls to jQuery ajaxSuccess?

Currently, I am working on creating an effect that involves a quick fade-out followed by a fade-in of the element once the request is successful. Since jQuery processes elements in a routine manner (top to bottom), I have managed to achieve my desired eff ...

Creating anonymous TypeScript class factories

Using TypeScript v2.2. In my codebase, there exists a class factory: export class A { name: string; } export function makeConstructor(name: string) { const newClass = class extends A { }; newClass.prototype.name = name; return newClass; } Howev ...

Adding real-time value to AngularJS directives without triggering reassessment

Seeking guidance on utilizing an AngularJS directive as an HTML element, with the intention to provide it two values. One will be fetched by iterating through a collection, while the other will be derived from that value: <mydirective myval="val" mymag ...

Having issues configuring CustomJacksonMapper to handle DateTime in Spring 4.1.5

Below are the configurations I've implemented: Configuration in my config file: <mvc:annotation-driven> <mvc:message-converters> <!-- Support for Joda Time --> <bean class="org.springframework.http.converter ...

Welcome Message using Discord.js V13

As a newcomer to bot creation, I am facing an issue with my welcome message in discord.js v13. Previously working in v12, I am now encountering difficulties sending a message to a specific channel when a user joins the server. After some investigation, I r ...

Reveal a concealed div upon submitting an HTML form to display the resulting output

I have created an HTML form to gather information about a person's name and location. Once the user submits the form, I want the output to be displayed at the bottom of the page using PHP echo. My specific requirement is, To initially hide the outp ...

How to extract and process multiple values of the same key in a JSON file using

I'm new to working with JSON and I'm a bit confused about something. What I am attempting to do is to parse the following data: [{"name":"Djinnibone"},{"name":"Djinnibutt","changedToAt":1413217187000},{"name":"Djinnibone","changedToAt":141321720 ...

Can we determine the drop location of an object in jQuery UI?

I am trying to determine the specific DOM element on a grid where an object was dropped, not just its x and y position. The grid is composed of div elements where various items can be dropped, and I need to identify the particular div where the item was dr ...

Issues arise in the alignment of the tab order when utilizing a combination of the stripe card

Experiencing an issue with tab index not working properly when using Vue.js and having a stripe card-element inside the Vue main element. Below is the code snippet: <html> <head> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js ...

Decoding a JSON array API response in Golang: A step-by-step guide

I have been attempting to extract data from Wikipedia's API at https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikipedia.org/all-access/all-agents/Smithsonian_Institution/daily/20160101/20170101 and convert it into an array of struc ...

Is it possible to invoke the function `this.function` within the same file where it is declared?

During our project, we set out to develop a Protractor framework. In the JavaScript file, we created some functions using the 'this.functionname' syntax with the intention of reusing these functions when needed within the same file. For better cl ...

Utilizing Chart.js to visualize live data from a dynamic MySQL dataset

Struggling to create a dynamic dataset for my Chart.js project, specifically with the data obtained from getpromorevenuechart.php. Here's an example of the dataset: [{"mmyy":"2019-12","promocode":"promo1","amount":"2776"},{"mmyy":"2020-01","promocode ...

Error happening outside of controllers or services but not being recorded

I've encountered an issue where exceptions occurring outside of controllers or services in plain JavaScript code are not being reported, do not appear in the console, and cannot be handled. For example, if there is an exception within a Car class that ...

The impact of placing a method in the constructor versus outside it within a class (yielding identical outcomes)

These two code snippets appear to produce the same result. However, I am curious about the differences between the two approaches and when it would be more appropriate to use one over the other. Can someone provide insight into this matter? Snippet 1: c ...

Encountering a persistent Unhandled rejection Error while utilizing NodeJs with Bluebird library

Currently in the process of developing a daemon that listens to TCP connections, sends commands, and listens for events. I made the decision to utilize bluebird to eliminate callbacks, but I'm encountering an issue. I can't seem to catch a rejec ...

"Did you come across `item()` as one of the methods within the array

While studying the book 'JavaScript and jQuery Interactive Front-End Web Development', I came across this interesting sentence: You can create an array using a different technique called an array constructor. This involves using the new keyword ...

Storing a collection of objects in session storage

I've been struggling to save an array containing the items in my online shopping cart. Even though both the object and the array are being filled correctly, when I check the sessionStorage, it shows an array with an empty object. I've spent a lot ...

What is the proper method for interpreting binary floating-point data when utilizing XMLHttpRequest?

I am attempting to load a binary file containing floating-point values into an array using JavaScript. Here is my current method: var mRequest = new XMLHttpRequest(); mRequest.open('GET', 'res/binary_float_data.bin'); mRequest.response ...