windows 10 pro office 2019 pro office 365 pro windows 10 home windows 10 enterprise office 2019 home and business office 2016 pro windows 10 education visio 2019 microsoft project 2019 microsoft project 2016 visio professional 2016 windows server 2012 windows server 2016 windows server 2019 Betriebssysteme office software windows server https://softhier.com/ instagram takipçi instagram beğeni instagram görüntüleme instagram otomatik beğeni facebook beğeni facebook sayfa beğenisi facebook takipçi twitter takipçi twitter beğeni twitter retweet youtube izlenme youtube abone instagram

Backbone + RequireJS + Mediator Pattern resulted in View Logic short-circuiting and infinite looping

Asked By: Anonymous

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?


Solution

Answered By: Anonymous

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.

techinplanet staff

instagram volgers kopen volgers kopen buy windows 10 pro buy windows 11 pro


Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno
Alanya escort Manavgat escort Fethiye escort Kemer escort Didim escort Çanakkale escort Aydın escort Muğla escort Tekirdağ escort Manisa escort Balıkesir escort Trabzon escort Elazığ escort Ordu escort Kütahya escort Isparta escort Rize escort Kahramanmaraş escort Yalova escort Giresun escort Yozgat escort Tokat escort Şanlıurfa escort Sivas escort Batman escort Erzurum escort Sinop escort Kırşehir escort Karaman escort Kırıkkale escort Bolu escort Amasya escort Niğde escort Uşak escort Edirne escort Çorum escort Osmaniye escort Zonguldak escort Van escort Erzincan escort Söke escort Bodrum escort Çerkezköy escort Akhisar escort Bandırma escort Ayvacık escort Akçaabat escort Karakoçan escort Altınordu escort Tavşanlı escort Eğirdir escort Ardeşen escort Afşin escort Altınova escort Bulancak escort Sorgun escort Erbaa escort Viranşehir escort Zara escort Kozluk escort Aziziye escort Ayancık escort Kaman escort Ermenek escort Keskin escort Gerede escort Göynücek escort Bor escort Banaz escort Havsa escort Osmancık escort Bahçe escort Alaplı escort Başkale escort Kemah escort Nazilli escort Fethiye escort Çorlu escort Alaşehir escort Altıeylül escort Biga escort Araklı escort Kovancılar escort Fatsa escort Simav escort Yalvaç escort Çayeli escort Dulkadiroğlu escort Çiftlikköy escort Espiye escort Sarıkaya escort Niksar escort Suruç escort Yıldızeli escort Sason escort Horasan escort Boyabat escort Mucur escort Sarıveliler escort Yahşihan escort Göynük escort Gümüşhacıköy escort Çamardı escort Eşme escort İpsala escort Sungurlu escort Hasanbeyli escort Çaycuma escort İpekyolu escort Refahiye escort Kuşadası escort Marmaris escort Süleymanpaşa escort Turgutlu escort Susurluk escort Gelibolu escort Of escort Ünye escort Domaniç escort Fındıklı escort Elbistan escort Çınarcık escort Tirebolu escort Akdağmadeni escort Turhal escort Eyyübiye escort Suşehri escort Yakutiye escort Gerze escort Mengen escort Merzifon escort Ulukışla escort Sivaslı escort Keşan escort Kadirli escort Ereğli escort Özalp escort Tercan escort Efeler escort Didim escort Çine escort Dalaman escort Menteşe escort Milas escort Ortaca escort Seydikemer escort Ergene escort Kapaklı escort Malkara escort Salihli escort Şehzadeler escort Soma escort Yunusemre escort Ayvalık escort Bigadiç escort Burhaniye escort Gönen escort Karesi escort Çan escort Yenice escort Ortahisar escort Yomra escort Perşembe escort Pazar escort Onikişubat escort Pazarcık escort Türkoğlu escort Eynesil escort Görele escort Piraziz escort Yağlıdere escort Çayıralan escort Boğazlıyan escort Zile escort Siverek escort Karaköprü escort Haliliye escort Akçakale escort Şarkışla escort Gemerek escort Oltu escort Palandöken escort Mudurnu escort Suluova escort Taşova escort Toprakkale escort Kilimli escort Tuşba escort Üzümlü escort