Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9050707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:43:19+00:00 2026-06-16T12:43:19+00:00

Update: Problems solved, case closed. I’m still having problems getting one part of my

  • 0

Update: Problems solved, case closed.


I’m still having problems getting one part of my code to work.

My view now listens to the collection for updates, and what should happen is:

  • ListView listens to Results Collection
  • Results are synced
  • ListView creates an ItemView for each Result
  • ListView (ul) appends each ItemView (li)

Everything seems to work fine, up until the final step.

The function in ListView that is supposed to add the results to a list does not have access to the ListView’s element.

I can create an ItemView, and retrieve it’s element “<li>“, but the ListView’s “<ul>” cannot be referred to within the function.

Sample code bits from ListView:

el: $('.result-list'),

initialize: function() {
   this.listenTo(this.collection, 'add', this.addOne);
},

addOne: function(result) {
   view = new ItemView({ model: result });
   this.$el.append(view.render().el);
},

In the above code, the variable view exists, as does it’s element, but “this” doesn’t refer to the ListView anymore.


Problem below solved

What I’m trying to accomplish is having a View module (search) able to trigger an event in a Collection (results).

When the Search View is submitted, it should pass the input field to the Collection’s fetch method to retrieve results from the server. Currently, I can trigger a function from the View, but the function does not have access to any of the Collection’s methods.

Previously, I had the View/Collection refer to each other directly by their variable names.
Since I have separated the code into modules, the View/Collection cannot access each other directly anymore.

Here is some of the code: (written in Coffeescript)

app.coffee – global_dispatcher is applied to Backbone

define [
    'jquery'
    'underscore'
    'backbone'
    'cs!router'
], ($, _, Backbone, Router) ->
    # global_dispatcher added to all Backbone Collection, Model, View and Router classes
    dispatcher = _.extend {}, Backbone.Events, cid: 'dispatcher'
    _.each [ Backbone.Collection::, Backbone.Model::, Backbone.View::, Backbone.Router:: ], (proto) ->
        _.extend proto, global_dispatcher: dispatcher

    new Router()

router.coffee – This is where I’m having trouble. The function for ‘getResults’ is triggered, but the collection ‘results’ is not accessible from here.

define [
    'backbone'
    'cs!views/resuls/list'
    'cs!views/results/search'
    'cs!collections/results'
], (Backbone, ListView, SearchView, Results) ->
    Backbone.Router.extend
        routes:
            # URL routes
            '': 'index'

        index: ->
            results = new Results
            new ListView { model: results }
            new SearchView

            @global_dispatcher.bind 'getResults', (data) ->
                console.log results

search.coffee – View which triggers the event, it will successfully trigger the event and pass the correct arguments.

define [
    'jquery'
    'backbone'
], ($, Backbone) ->
    Backbone.View.extend
        events:
            'submit #search-form': 'submit'

        submit: (evt) ->
            evt.preventDefault()

            phrase = @.$('input').val()
            @.$('input').val('')

            args = name: phrase

            @global_dispatcher.trigger 'getResults', args
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T12:43:20+00:00Added an answer on June 16, 2026 at 12:43 pm

    If I’m understanding your problem correctly it’s not hard to solve. Here’s some dummy code to illustrate:

    var Results = Backbone.Collection.extend();
    var Search = Backbone.View.extend({
        someEventHandler: function() {
            // The next line accesses the collection from the view
            this.collection.fetch();
        }
    });
    var results = new Results();
    var search = new Search({collection: results});
    

    If you want your view to do something after the results come back, just bind an event handler on the collection:

    var Search = Backbone.View.extend({
        fetchResposne: function() { /* do something*/},
        someEventHandler: function() {
            // The next line accesses the collection from the view
            this.collection.on('sync', this.fetchResponse);
            this.collection.fetch();
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE: First problem solved, second one described at the bottom of this post. UPDATE2:
I am having problems using the update method of scala.collection.immutable.HashMap.I don't see the reason
UPDATE: I've solved this now - see my own comment. Amazing how often writing
I'm having problems with the PHP site I run at work where users are
Update II Problem Solved but Why? This has been the biggest headache ever. My
The problem is not solved although I accepted one answer. Problem: Vim updates very
I've had problems with the update method in subsonic, so instead of using: a.Update()
UPDATE: Problem located in my related question - Nokogiri performance problem I am having
I am having a binding update problem with my ComboBox. My ComboBox's ItemSource is
Spent the good part of this morning working through this, but still…. apologies if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.