The Mediator pattern enables communication (mediation) between views using a mediator object.
The mediator inside is extend Backbone mediator and come with Oro Platform in the path vendor/oro/platform/src/Oro/Bundle/UIBundle/Resources/public/js/mediator.js , So you can use by calling inside your custom js by :
var mediator = require('oroui/js/mediator');
So the mediator trigger many events like :
mediator.trigger('cms:content-variant-collection:add', this.$el); mediator.trigger('checkout-content:updated');
Is clean trigger every new content variations added in Content block , the second trigger any content update in checkout step .
So exist too some other with after and before like:
mediator.trigger('checkout-content:before-update');
How you can Listen Event with mediator:
first method is using on function:
initialize: function() { mediator.on('checkout-content:updated', this._onContentUpdated, this); }
and add you function :
_onContentUpdated: function() { //some code; }
to stop trigger:
dispose: function() { if (this.disposed) { return; } mediator.off('checkout-content:updated', this._onContentUpdated, this); }
Second technique:
listen: { 'grid_load:complete mediator': 'onGridLoadComplete' }
and add you function:
onGridLoadComplete: function(collection) { //some code }
Full example of Listening add to shopping cart:
'use strict'; var AddShoppingListDetectorComponent; var BaseComponent = require('oroui/js/app/components/base/component'); var mediator = require('oroui/js/mediator'); var routing = require('routing'); var _ = require('underscore'); AddShoppingListDetectorComponent = BaseComponent.extend({ /** * @inheritDoc */ listen: { 'shopping-list:line-items:update-response mediator': '_onLineItemAdd' }, /** * @inheritDoc */ constructor: function AddShoppingListDetectorComponent() { AddShoppingListDetectorComponent.__super__.constructor.apply(this, arguments); }, _onLineItemAdd: function(model, response) { //Some code } }); return AddShoppingListDetectorComponent; });
Is simple code .
Comments