javascript - Marionette - listenTo a custom event triggered from a collection -


i'm using event aggregator(ea) keep track of events in app. there 1 situation not make ea listen custom event triggered collection. see more details below.

emgr (event manager i.e event aggregator):

define(['backbone.wreqr'],function(wreqr){     "use strict";     return new wreqr.eventaggregator(); }) 

collection:

define([ 'underscore',  'backbone', 'emgr', 'models/mainmenu/mm.model.category'], function(_, backbone, emgr, m_category){   var categoriescollection = backbone.collection.extend({     model: m_category,      initialize: function(options) {         console.log('categoriescollectionview initialized...');         this.url= 'test1';         this.fetch().then(function(){             emgr.trigger("test1")         })     }    });    return categoriescollection; }); 

layoutview:

define([ 'backbone', 'underscore', 'marionette', 'emgr',             'templates/template.mainmenu',              'collections/mainmenu/mm.collection.categories',             'layouts/mainmenu/collectionview.categories'    ],     function (backbone, _, marionette, emgr, template, c_categories, cv_categories) {     "use strict";     var categorylayoutview = marionette.layoutview.extend({          template: template['categories.layout'],          regions: {             categories : '#categories'         },          id: 'categories-layout',          initialize: function(options){             var r_categories = this.categories;             this.listento(emgr, "test1", this.test);             this.categories = new c_categories(options)         },          test: function(){             console.log("test");             var categories_layout = new cv_categories({ collection: this.categories});             categories_layout.render()         },          onrender: function(){         },          onshow: function(){          }     });      return categorylayoutview; }); 

as can see above, i'm trying achieve render collectionview (cv_categories) once collection has finished loading data.

however doesn't work unless listen events follows:

emgr.listento(emgr, "test1", this.test); 

this trigger function , although i'm no longer able access layoutview's methods/variables , on. can't access collection, , therefore not able initiate collectionview properly.

now question is, doing wrong or work intended? there other way accomplish this?

you emgr.on("test1", this.test, this) execute this.test this context (note third parameter).this way keep access view "this" scope , therefore view's functions.

if prefer use listento, emgr.listento(emgr, "test1", this.test.bind(this)). find more redundant , unnecesarily verbose, though.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -