Các hệ thống đối tượng dựa trên RequireJS của Magento 2 chứa một tính năng gọi là "mixins". Một mixin Magento 2 không phải là thứ mà một kỹ sư phần mềm thường nghĩ là một hỗn hợp / đặc điểm . Thay vào đó, mixin Magento 2 cho phép bạn sửa đổi đối tượng / giá trị được trả về bởi mô-đun RequireJS trước khi đối tượng / giá trị đó được sử dụng bởi chương trình chính. Bạn định cấu hình một mixin Magento 2 như thế này (thông qua tệp requestjs-config.js)
var config = {
'config':{
'mixins': {
//the module to modify
'Magento_Checkout/js/view/form/element/email': {
//your module that will do the modification
'Pulsestorm_RequireJsRewrite/hook':true
}
}
}
};
Sau đó, bạn cần phải có hook.js(hoặc bất kỳ mô-đun RequireJS nào bạn đã định cấu hình),
define([], function(){
console.log("Hello");
return function(theObjectReturnedByTheModuleWeAreHookingInto){
console.log(theObjectReturnedByTheModuleWeAreHookingInto);
console.log("Called");
return theObjectReturnedByTheModuleWeAreHookingInto;
};
});
trả về một hàm Magento sẽ gọi chức năng này, chuyển tham chiếu đến "mô-đun" mà bạn muốn sửa đổi. Trong ví dụ của chúng tôi, đây sẽ là đối tượng được trả về bởi mô-đun RequireJS Magento_Checkout/js/view/form/element/email. Đây cũng có thể là một hàm hoặc thậm chí là một giá trị tỷ lệ (tùy thuộc vào những gì mô-đun RequireJS trả về).
Hệ thống này dường như được gọi mixinsvì nó cho phép bạn tạo hành vi giống như mixin nếu đối tượng được trả về bởi mô-đun RequireJS ban đầu hỗ trợ extendphương thức.
define([], function(){
'use strict';
console.log("Hello");
var mixin = {
ourExtraMethod = function(){
//...
}
};
return function(theObjectReturnedByTheModuleWeAreHookingInto){
console.log(theObjectReturnedByTheModuleWeAreHookingInto);
console.log("Called");
return theObjectReturnedByTheModuleWeAreHookingInto.extend(mixin);
};
});
Tuy nhiên, bản thân hệ thống chỉ là một cách để nối vào việc tạo đối tượng mô-đun.
Preamble xong - không ai biết cách Magento đã thực hiện chức năng này? Trang web RequireJS dường như không đề cập đến mixins (mặc dù Google nghĩ rằng bạn có thể muốn trang plugin của RequireJS ).
Ngoài các requirejs-config.jstệp, javascript lõi của Magento 2 chỉ đề cập đến mixinsba tệp
$ find vendor/magento/ -name '*.js' | xargs ack mixins
vendor/magento/magento2-base/lib/web/mage/apply/main.js
73: if (obj.mixins) {
74: require(obj.mixins, function () {
79: delete obj.mixins;
vendor/magento/magento2-base/lib/web/mage/apply/scripts.js
39: if (_.has(obj, 'mixins')) {
41: data[key].mixins = data[key].mixins || [];
42: data[key].mixins = data[key].mixins.concat(obj.mixins);
43: delete obj.mixins;
vendor/magento/magento2-base/lib/web/mage/requirejs/mixins.js
5:define('mixins', [
24: * Adds 'mixins!' prefix to the specified string.
30: return 'mixins!' + name;
76: * Iterativly calls mixins passing to them
80: * @param {...Function} mixins
84: var mixins = Array.prototype.slice.call(arguments, 1);
86: mixins.forEach(function (mixin) {
96: * Loads specified module along with its' mixins.
102: mixins = this.getMixins(path),
103: deps = [name].concat(mixins);
111: * Retrieves list of mixins associated with a specified module.
114: * @returns {Array} An array of paths to mixins.
118: mixins = config[path] || {};
120: return Object.keys(mixins).filter(function (mixin) {
121: return mixins[mixin] !== false;
126: * Checks if specified module has associated with it mixins.
137: * the 'mixins!' plugin prefix if it's necessary.
172: 'mixins'
173:], function (mixins) {
237: deps = mixins.processNames(deps, context);
252: queueItem[1] = mixins.processNames(lastDeps, context);
Các mixins.jstập tin dường như là một RequireJS cắm (dựa trên !...đề cập đến trong các ý kiến - là quyền này) nhưng nó không phải là 100% rõ ràng khi main.jshay scripts.jsđược gọi bởi Magento, hoặc làm thế nào các tùy chỉnh mixinscấu hình làm cho nó từ requirejs-config.jsvào người nghe hệ thống / móc miêu tả trên.
Có ai có một lời giải thích cho cách hệ thống này được / được thực hiện / kiến trúc, với một mắt hướng tới khả năng gỡ lỗi tại sao một "mixin" có thể hoặc không thể được áp dụng?
mixinscấu hình làm gìx-magento-initvàdata-mage-initcấu hình? tức là - trong ví dụ trên của bạn,path/to/configuration-modifiercũng sẽ trả về một cuộc gọi lại có thể sửa đổi dữ liệu cấu hình? Hay cái gì khác?