SFRA - Some of the middleware events are not firing #415
Replies: 6 comments
-
Hi @amitkathpal123 , In your code you always replace the viewData and never keep the value of attributes set by previous event handler calls. This is why in the end you see only single attribute, which is set from the last event handler executed. You need to follow the following code pattern to get the result you are probably expecting: this.on('route:Start', function (req, res) {
var viewData = res.getViewData();
viewData.valueStart = 'This is the text from event Route:Start';
res.setViewData(viewData);
}); Hope this helps, |
Beta Was this translation helpful? Give feedback.
-
Thanks @zlatinz . I tried but it dint make any difference. |
Beta Was this translation helpful? Give feedback.
-
Just checked, and you are right, setViewData uses assign.js to do a copy of all viewData object properties. Have you tried this? // Controller Function
var eventEmitters = server.get('EventEmitters',
testmw.testMWFunc,
server.middleware.https,
function(req, res, next) {
res.render("eeTemplate",{value:'This text is from Main Function'})
next();
}
);
eventEmitters.on('route:Start', function (req, res) {
res.setViewData({valueStart:'This is the text from event Route:Start'});
});
eventEmitters.on('route:Step', function (req, res) {
res.setViewData({valueStep:'This is the text from event Route:Step'});
});
eventEmitters.on('route:BeforeComplete', function (req, res) {
res.setViewData({valueBeforeComplete:'This is the text from event Route:BeforeComplete'});
}); |
Beta Was this translation helpful? Give feedback.
-
Thanks @zlatinz . This worked but wondering what difference it made. |
Beta Was this translation helpful? Give feedback.
-
Hi @amitkathpal123 , // Controller Function
var eventEmitters = server.get('EventEmitters',
testmw.testMWFunc,
server.middleware.https,
function(req, res, next) {
this.on('route:Start', function (req, res) {
res.setViewData({valueStartInside:'This is the text from event Route:Start'});
});
res.render("eeTemplate",{value:'This text is from Main Function'})
next();
}
);
eventEmitters.on('route:Start', function (req, res) {
res.setViewData({valueStartOutside:'This is the text from event Route:Start'});
}); The sequence of execution is (simplifying things a bit):
|
Beta Was this translation helpful? Give feedback.
-
@zlatinz . Thank you. This clarifies and helps. |
Beta Was this translation helpful? Give feedback.
-
Hi All,
I am testing to fire MW events (step, start, beforeComplete) by calling a MW function.
Can you please check below code and point me to the mistake or if anyone has sample code where all events are working will be helpful.
// Controller Function
server.get('EventEmitters', testmw.testMWFunc, server.middleware.https, function(req, res, next){
});
Thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions