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

  • SEARCH
  • Home
  • 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 8533835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:03:23+00:00 2026-06-11T10:03:23+00:00

I am currently using Backbone.Mediator to leverage the benefit of Mediator Pattern in my

  • 0

I am currently using Backbone.Mediator to leverage the benefit of Mediator Pattern in my Backbone + RequireJS project; however, I encountered a peculiar behaviour of the Pattern which am not so sure whether it’s “by-design”, or rather, not the standard behaviour of Mediator Pattern but a bug in the plugin.

As a contrived example:

AMD Module 1

var View1 = Backbone.View.extend({
    ...
    events: {
        'click div: switchList'
    },
    switchList: function() {
        Backbone.Mediator.pub('list:switch');
    }
});

AMD Module 2

var View2 = Backbone.View.extend({
    ...
    subscriptions: {
        'list:switch': 'shrinkDiv'
    },
    shrinkDiv: function() {
        Backbone.Mediator.pub('div:shrink');
        this.shrinkAndMore();
    }
});

return View2;

AMD Module 3

define(function(require) {
    var View2 = require(**AMD Module 2**);

    var View3 = Backbone.View.extend({
        ...
        subscriptions: {
            'div:shrink': 'createSiblingDiv'
        },
        createSiblingDiv: function() {
            this.siblingView = new View2();
            this.$el.after(this.siblingView.$el);
            this.siblingView.render();
        }
    });
});

I thought it would work like this:

                      **View1**.switchList();
                      │
Channel 'list:switch' │
                      ↓
                      **View2**.shrinkDiv();
                      │
                      ├─┐
                      │ │ Channel 'div:shrink'
                      │ ↓
                      │ **View3**.createSiblingDiv();
                      │ │
                      │ └──→ "SiblingDiv created and rendered"
                      │
                      └────→ "View2 Div shrinked and more"

However, the truth is since SiblingDiv is another instance of View2 which subscribes to Channel ‘list:switch’, SiblingDiv, immediately after its creation, will also be triggered by the event signal still transpiring over Channel ‘list:switch’ (which will only cease after the execution of **View2**.shrinkAndMore();).

So the real code flow looks like this:

                      **View1**.switchList();
                      │
Channel 'list:switch' │
                      ↓
                      **View2**.shrinkDiv(); ←──────────────────┐
                      │                                         │
                      ├─┐                                       │
                      │ │ Channel 'div:shrink'                  │
                      │ ↓                                       │
                      │ **View3**.createSiblingDiv();           │
                      │ │                                       │
                      │ └──→ "SiblingDiv created and rendered" ─┘
                      │
                      └────→ "View2 Div shrinked and more"

An infinite loop… Ooops!

I was able to make things working my way with some modifications to my code:

AMD Module 2 Modded

var View2 = Backbone.View.extend({
    initialize: function() {                                 // new code
        if (this.options.subscriptions) {                    // new code
            this.subscriptions = this.options.subscriptions; // new code
        }                                                    // new code
    },                                                       // new code
    ...
    subscriptions: {
        'list:switch': 'shrinkDiv'
    },
    shrinkDiv: function() {
        Backbone.Mediator.pub('div:shrink');
        this.shrinkAndMore();
    }
});

return View2;

AMD Module 3 Modded

define(function(require) {
    var View2 = require(**AMD Module 2**);

    var View3 = Backbone.View.extend({
        ...
        subscriptions: {
            'div:shrink': 'createSiblingDiv'
        },
        createSiblingDiv: function() {
            this.siblingView = new View2({        // new code
                subscriptions: {}                 // new code
            });                                   // new code
            this.$el.after(this.siblingView.$el);
            this.siblingView.render();
        }
    });
});

But I am very interested to understand that whether the infinite looping behaviour (new Object created during an event signal broadcasting will also be triggered by that very signal) is considered “standard” in Mediator Pattern methodology? Or is this all just a bug on the plugin part?

  • 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-11T10:03:30+00:00Added an answer on June 11, 2026 at 10:03 am

    Seems this is a bug in the plugin. Take a look a this line. It will loop over all events that was registered in this channel. The problem is that it stops when every registered events are called and that it checks for the lengths of the array of the registered events in every loop step. So whats happens is:

    1. you fire the event
    2. your registered event is called
    3. an new instance is created and register itself to the channel
    4. this increase the length of the array by one
    5. so instead of ending the loop it will call the listener of the just created view
    6. go back to 3.

    Changing the line to:

    for (var i = 0, l =  channels[channel].length; i < l; i++) {
    

    should fix this cause you get the length of the array at the beginning. Adding new element will not end in an infinit loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently using backbone boilerplate and running it on my MAMP server localhost. I
I'm currently using the NEST ElasticSearch C# Library for interacting with ElasticSearch. My project
I am using backbone to build my web app. Currently I am facing an
Currently using: @^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&amp;%\$#_]*)?$ How can i make http:// not compulsory but if it does
I have a web application that is currently using backbone.js 0.5.3 with backbone local
I'm working on an app that uses Backbone and RequireJS (using the Backbone boilerplate).
I am currently developing an application using Phonegap / Jekyll / Backbone. I am
Currently transitioning a Rails 3.2+ app over to using Backbone.js to manage its front-end.
I'm currently using Handlebars.js (associated with Backbone and jQuery) to make a web app
Currently I am using backbone-rails in my Rails app. I would like to know

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.