0
votes

So, I have one view(marionette itemview) which is to render the google maps based on the address given as input.This view is being used in three places of the application.Where as in two places,

Case 1: I'm using regions to show the view(like: xyzregion.show(new mapView().render());),

Case 2: I'm not using region, instead I'm doing like: this.el.innerHTML = new mapView().render();.

Though marionette documentation has mentioned that ItemView, CompositeView, collectionView, LayoutView( Docs) does supports this functionality of onShowwhen,If I don't use the region to show the map view, onShow is not being triggered. After some research I came to know that "view itself won't trigger the onShow, instead it triggered by region.

Is there any way to trigger an "onShow", even though view is not bieng shown by region? or else is there any other method which does exactly same as "onShow"?

1
onRender or onAttach ? - ivarni
I did not understand this statement The attach event is only fired when the view becomes a child of the document in marionette documemt. - Swathi
onRender is called after the view's HTML is being generated while onAttach gets called after it's acutally been placed into the document so that's most likely the closest one to onShow - ivarni
FYU, this map view is not child view. as per the documentation 'onShow' should also work right?, but it's not, that means documentation is wrong!?. - Swathi
and I treid with onAttach also, but same problem as with onShow, it is being triggered only when view is being shown by the region, for Case 2 it is not triggering onAttach @ivarni. - Swathi

1 Answers

1
votes

It sounds like you just want to write your own custom implementation of the pub-sub pattern.

Since you're using Marionette, you can just use

vent = new Backbone.Marionette.EventAggregator();

which is an object that can broadcast / listen to events.

Alternatively, you can define a model instance and send this to the view:

var View = Backbone.View.extend({ 
    initialize: function(){
        this.model.on("change:address", this.render, this); 
    }, 
    render: function(){ //Render }
}); 

var someModel = new SomeBBModel(); 
var view = new View({ model: someModel }); 

Elsewhere in your code:

//Set address and trigger change 
model.set("address", "bla"); 

For activating Google Maps, it is necessary for the div to be in the DOM, which is why the onShow callback might come in handy though.
For panning the map, you can just define another method on the view that takes care of this when the address changes.