I'm trying to call a Magento Javascript Component in my phtml page to get the customer data saved in session.
I'm having an issue in that the customerdata constructor gets loaded after my script and I can't figure out how to get my script to run after.
This is the customer-data.js file that needs to get loaded before my script
'Magento_Customer/js/customer-data': function (settings) {
options = settings;
invalidateCacheBySessionTimeOut(settings);
invalidateCacheByCloseCookieSession();
customerData.init();
}
Here is how I'm trying to call it
require([
'jquery',
'Magento_Customer/js/customer-data',
'domReady!',
'mage/loader'
], function (jQuery,customerData) {
//<![CDATA[
jQuery(document).ready(function() {
var data = customerData.get('mysession-data');
});
});
My code works when the customerdata gets initialized but it's not always getting initialized first.
Is there anyway to configure this to get the customerdata to initialize and then I can call my session data?
-
have you find a way to resolve this case?LucScu– LucScu2017年09月22日 10:42:53 +00:00Commented Sep 22, 2017 at 10:42
2 Answers 2
You can try to use RequireJs mixins in order to hook the core function.
var config = {
'config':{
'mixins': {
'Magento_Customer/js/customer-data': {
'Vendor_Module/hook.js':true
}
}
}
};
Alan Storm described it here : http://alanstorm.com/the-curious-case-of-magento-2-mixins/
You can try to use shim option with requirejs as explain here :
-
thanks. I don't really want to alter an existing function. I just want to get the content of customerData after it's initialized. I think the script that runs the "text/x-magento-init" must happen after my codejstrez77– jstrez772017年01月03日 04:07:26 +00:00Commented Jan 3, 2017 at 4:07
-
Do you try shim option ? The jQuery ready is not require I think.Franck Garnier– Franck Garnier2017年01月04日 07:06:59 +00:00Commented Jan 4, 2017 at 7:06
Add this in
// Vendor/Module/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="1.0.0" >
<sequence>
<module name="Magento_Customer"/>
</sequence>
</module>
</config>
This basically tells magento to load Magento_Customer module first before running your module, so by then customer-data will be available.