Spinal cord: Connecting to the surrounding container of the view

Within my Backbone IndexView, I am using a TaskView for each 'task' model. I would like to bind an event to the li element that encloses the taskview. For instance, if the 'className' attribute is 'task', I want to trigger an event on '.task'.

I am able to bind from the parent (IndexView) view, but I want to have access to information inside the clicked view, which I assume means the event should be bound to the child TaskView(?)

(Additionally, I can access DOM classes within the actual tasks template...just not the wrapping .task li)

Any suggestions are welcome - see code snippet below.

CHILD VIEW

Backbonescaffolddemo.Views.Tasks ||= {}

class Backbonescaffolddemo.Views.Tasks.TaskView extends Backbone.View
  template: JST["backbone/templates/tasks/task"]

  tagName: "li"
  className: "task"

  events:
    "click .task"    : "demoMethod"

  initialize: () ->
    _.bindAll(this, 'demoMethod', 'render')
    console.log @
    #@showTask()
    @el.id = 'sort_order_' + @model.get('id') if @model

  destroy: () ->
    @model.destroy()
    this.remove()
    return false

  demoMethod: () ->
    console.log 'Work dammit, work. Arghhh'

  render: ->
    $(@el).html(@template(@model.toJSON() ))    
    return this

PARENT VIEW

Backbonescaffolddemo.Views.Tasks ||= {}

class Backbonescaffolddemo.Views.Tasks.IndexView extends Backbone.View
  template: JST["backbone/templates/tasks/index"]
  id: "taskList"

  events: 
    "keyup #searchTasks" : "searchTasks"

  initialize: () ->
    _.bindAll(this, 'addOne', 'sortable', 'task_id_from_element', 'addAll', 'searchTasks', 'updateSort', 'sortable', 'render')
    @options.tasks.bind('reset', @addAll)
    @options.tasks.bind('reset', @sortable)
    @options.tasks.bind('add', @render)
    @updateSort()
    @sortable()

  #...

  addAll: () ->
    @options.tasks.each(@addOne)

  addOne: (task) ->
    view = new Backbonescaffolddemo.Views.Tasks.TaskView({model: task})
    $(@el).append(view.render().el)

  render: () ->
    $(@el).html(@template(tasks: @options.tasks.toJSON() ))
    @addAll() 
    return this

Answer №1

Success! After some searching, I discovered that simply clicking on "demoMethod" without specifying a DOM scope gets the job done.

Answer №2

Could you please clarify where the div enclosing the TaskView is being included?

Typically, Backbone views are enclosed within a div tag by default. However, if you have specified the tagName in your TaskView as li, then it will be wrapped in an li tag instead of a div.

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

Can you explain the process of initiating a script?

Seeking Answers: 1) When it comes to initializing a script, is there specific code placement in the js file or do you need to create initialization code from scratch? 2) What sets jQuery apart from other scripts that require activation - why does jQuery. ...

Conceal form after submission - Django 1.6

I'm currently working on a Django 1.6 project where I have this form: <form action="/proyecto/" method="POST" id="myform"> {% csrf_token %} <table> <span class="Separador_Modulo">& ...

What is the best way to combine 2 javascript objects to create a JSON structure without any nested levels?

I am having an issue with my mock server setup. I am using npm faker to generate random data, and then trying to transfer that data to another JavaScript file. My goal is to have a JSON structure like the following: { 0: varOne: 'value' ...

Utilizing custom links for AJAX to showcase targeted pages: a beginner's guide

I am putting the final touches on my website and realized that it may be difficult to promote specific sections of the site because the browser address never changes. When visitors click on links, they open in a div using Ajax coding. However, if I want to ...

Jquery Plugin fails to generate dynamic elements effectively

I developed a masking jQuery script that dynamically adds elements to an existing input element. var link = $('<a title="show" role="link" href="#" class="masker-value">show</a>'); wrapper: function() { container = $(container) ...

Error in Typescript for the prop types of a stateless React component

When reviewing my project, I came across the following lines of code that are causing a Typescript error: export const MaskedField = asField(({ fieldState, fieldApi, ...props }) => { const {value} = fieldState; const {setValue, set ...

`How can I activate caching for getServerSideProps in Next.js?`

We have implemented server-side rendering for a few pages and components. In an attempt to optimize performance, we have been experimenting with caching API responses. export async function getServerSideProps(context) { const res = await getRequest(API ...

What could be causing my React Router to fail in displaying the content of Home.js?

My <Route> is not functioning as expected The Route leading to the homepage does not show the content from the Home.js file import "./App.css"; import Navbar from "./components/Navbar"; import { BrowserRouter as Router, Route, Ro ...

The application is unable to recognize the CSS file

I am facing an issue where the design of my app is not displaying, even though the CSS file is located in the same folder. I'm unsure about what mistake I might have made! import React from 'react'; import classes from './Burger.css&ap ...

dependency in useEffect hook not being properly updated

Currently, I am implementing an API call within the useEffect hook in the following manner: useEffect(() => { fetchPatientsStartAsync(patientListUrl); }, [patientListUrl]); Moreover, in my codebase I have two additional state variables and a method in ...

JavaScript Promise Chains- Issues with Functionality?

Could someone provide an explanation for why calling secondMethod in the Promise chain yields results, while calling secondMethod() does not? function firstFunction() { return new Promise(function(resolve, reject){ setTimeout(function() { ...

AngularJS property sorting: organize your list by name

I have a complicated structure that resembles: { 'street35':[ {'address154': 'name14'}, {'address244': 'name2'} ], 'street2':[ {'address15& ...

Searching for a RegEx expression or jQuery selector that will exclude "external" links in the href attribute

I am using a custom jQuery plugin to enable Ajax loading of page content when certain links are clicked. Implementing this is straightforward with a delegated event like $(document).on('click','a', function(){});. However, I want this ...

Analyzing all the <option> elements within a <select> tag with jQuery

I have a WordPress plugin in development and I am currently facing an issue with comparing user-input from an <input> element to a set of <option> elements within a <select> element. Here is my current approach: $('button#test_bu ...

Schema for JSON object with dynamically generated keys

I have a specific request that follows a certain format: { "uuid_1": [{obj}, {obj}, {obj}], "uuid_2": [{obj}, {obj}], ... "uuid_n": [{obj}, {obj}], } The object 'obj' is a simple object that contains a f ...

Leverage the power of AJAX and PHP to securely save comments for future

I have coded a JavaScript function that uses POST and GET methods to send comments from an input field and retrieve them when the page reloads. However, I am unsure of how to handle the data after it is sent in order to save it and access it again later. E ...

The issue with the transition element not functioning properly in the hamburger menu is being encountered while using Tailwind CSS alongside

I am currently working on creating a responsive navigation bar in nuxt3/vue. <template> <nav class="flex justify-between items-center w-[92%] mx-auto gap-4 border border-2 border-black p-2" > <div> <span cla ...

unable to adjust the maximum height limit

I've been struggling to set a maximum height on the slider I'm currently using. No matter what height value I input, it doesn't seem to take effect. Additionally, I attempted setting the width for the echo img row in the PHP section but enco ...

Having difficulty adding items to the shopping cart

While working on a simple checkout system using django and react, I encountered an issue. Upon clicking the add to cart button, instead of adding the item to the cart as intended, I receive a 404 page not found error. I suspect that the problem may lie in ...

Turn off HTML5 Audio

There are 4 <audio> elements on a webpage. The client has asked for them to be available sequentially. Meaning, the first audio should play, then the rest should remain disabled until the first one finishes, and so on. In addition, they want the au ...