Magento 2 knockout component scope
I have two knockout components. The xml in layout is:
<body>
<referenceContainer name="content">
<block class="VendorModuleBlockList" name="some_name" template="list.phtml">
<arguments>
<argument name="jsLayout" xsi:type="array">
<item name="components" xsi:type="array">
<item name="js_list" xsi:type="array">
<item name="component" xsi:type="string">Vendor_Module/js/js-list</item>
<item name="config" xsi:type="array">
<item name="template" xsi:type="string">Vendor_Module/js-list</item>
</item>
<item name="children" xsi:type="array">
<item name="js_item" xsi:type="array">
<item name="component" xsi:type="string">Vendor_Module/js/js-item</item>
<item name="config" xsi:type="array">
<item name="template" xsi:type="string">Vendor_Module/js-item</item>
</item>
</item>
</item>
</item>
</item>
</argument>
</arguments>
</block>
</referenceContainer>
</body>
The component js_list has a template that contains the fallowing:
<div data-bind="foreach: someArray, visible: someArray().length > 0 ">
<p data-bind="scope: 'js_list.js_item'">
<!-- ko template: getTemplate() --><!-- /ko -->
<p data-bind="text: testVarThatPrintsTest"></p>
</p>
</div>
The content of the template from the component js_item is printed out but when I try to access the variable defined in it testVarThatPrintsTest :
define(
['jquery', 'ko', 'uiComponent', 'Magento_Ui/js/modal/modal', 'domReady!'],
function($, ko, Component, modal) {
'use strict';
return Component.extend({
initialize: function() {
this._super();
},
defaults: {
template: 'Vendor_Module/js-item',
testVarThatPrintsTest: 'TEST TEST TEST@'
}
});
}
);
I get the fallowing error:
Message: testVarThatPrintsTest is not defined
at text (eval at createBindingsStringEvaluator (knockout.js:2624), <anonymous>:3:57)
at update (knockout.js:4260)
at ko.dependentObservable.disposeWhenNodeIsRemoved (knockout.js:3004)
at evaluateImmediate (knockout.js:1737)
at Object.ko.computed.ko.dependentObservable (knockout.js:1946)
at knockout.js:3002
at Object.arrayForEach (knockout.js:151)
at applyBindingsToNodeInternal (knockout.js:2974)
at applyBindingsToNodeAndDescendantsInternal (knockout.js:2854)
at Object.ko.applyBindings (knockout.js:3065)
What is the problem here?
magento-2.1 uicomponent knockoutjs
bumped to the homepage by Community♦ 11 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I have two knockout components. The xml in layout is:
<body>
<referenceContainer name="content">
<block class="VendorModuleBlockList" name="some_name" template="list.phtml">
<arguments>
<argument name="jsLayout" xsi:type="array">
<item name="components" xsi:type="array">
<item name="js_list" xsi:type="array">
<item name="component" xsi:type="string">Vendor_Module/js/js-list</item>
<item name="config" xsi:type="array">
<item name="template" xsi:type="string">Vendor_Module/js-list</item>
</item>
<item name="children" xsi:type="array">
<item name="js_item" xsi:type="array">
<item name="component" xsi:type="string">Vendor_Module/js/js-item</item>
<item name="config" xsi:type="array">
<item name="template" xsi:type="string">Vendor_Module/js-item</item>
</item>
</item>
</item>
</item>
</item>
</argument>
</arguments>
</block>
</referenceContainer>
</body>
The component js_list has a template that contains the fallowing:
<div data-bind="foreach: someArray, visible: someArray().length > 0 ">
<p data-bind="scope: 'js_list.js_item'">
<!-- ko template: getTemplate() --><!-- /ko -->
<p data-bind="text: testVarThatPrintsTest"></p>
</p>
</div>
The content of the template from the component js_item is printed out but when I try to access the variable defined in it testVarThatPrintsTest :
define(
['jquery', 'ko', 'uiComponent', 'Magento_Ui/js/modal/modal', 'domReady!'],
function($, ko, Component, modal) {
'use strict';
return Component.extend({
initialize: function() {
this._super();
},
defaults: {
template: 'Vendor_Module/js-item',
testVarThatPrintsTest: 'TEST TEST TEST@'
}
});
}
);
I get the fallowing error:
Message: testVarThatPrintsTest is not defined
at text (eval at createBindingsStringEvaluator (knockout.js:2624), <anonymous>:3:57)
at update (knockout.js:4260)
at ko.dependentObservable.disposeWhenNodeIsRemoved (knockout.js:3004)
at evaluateImmediate (knockout.js:1737)
at Object.ko.computed.ko.dependentObservable (knockout.js:1946)
at knockout.js:3002
at Object.arrayForEach (knockout.js:151)
at applyBindingsToNodeInternal (knockout.js:2974)
at applyBindingsToNodeAndDescendantsInternal (knockout.js:2854)
at Object.ko.applyBindings (knockout.js:3065)
What is the problem here?
magento-2.1 uicomponent knockoutjs
bumped to the homepage by Community♦ 11 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
remove comma at the end from string testVarThatPrintsTest: 'TEST TEST TEST@',
– Rakesh Jesadiya
Nov 13 '17 at 10:33
that is not the problem
– Lachezar Raychev
Nov 13 '17 at 10:36
add a comment |
I have two knockout components. The xml in layout is:
<body>
<referenceContainer name="content">
<block class="VendorModuleBlockList" name="some_name" template="list.phtml">
<arguments>
<argument name="jsLayout" xsi:type="array">
<item name="components" xsi:type="array">
<item name="js_list" xsi:type="array">
<item name="component" xsi:type="string">Vendor_Module/js/js-list</item>
<item name="config" xsi:type="array">
<item name="template" xsi:type="string">Vendor_Module/js-list</item>
</item>
<item name="children" xsi:type="array">
<item name="js_item" xsi:type="array">
<item name="component" xsi:type="string">Vendor_Module/js/js-item</item>
<item name="config" xsi:type="array">
<item name="template" xsi:type="string">Vendor_Module/js-item</item>
</item>
</item>
</item>
</item>
</item>
</argument>
</arguments>
</block>
</referenceContainer>
</body>
The component js_list has a template that contains the fallowing:
<div data-bind="foreach: someArray, visible: someArray().length > 0 ">
<p data-bind="scope: 'js_list.js_item'">
<!-- ko template: getTemplate() --><!-- /ko -->
<p data-bind="text: testVarThatPrintsTest"></p>
</p>
</div>
The content of the template from the component js_item is printed out but when I try to access the variable defined in it testVarThatPrintsTest :
define(
['jquery', 'ko', 'uiComponent', 'Magento_Ui/js/modal/modal', 'domReady!'],
function($, ko, Component, modal) {
'use strict';
return Component.extend({
initialize: function() {
this._super();
},
defaults: {
template: 'Vendor_Module/js-item',
testVarThatPrintsTest: 'TEST TEST TEST@'
}
});
}
);
I get the fallowing error:
Message: testVarThatPrintsTest is not defined
at text (eval at createBindingsStringEvaluator (knockout.js:2624), <anonymous>:3:57)
at update (knockout.js:4260)
at ko.dependentObservable.disposeWhenNodeIsRemoved (knockout.js:3004)
at evaluateImmediate (knockout.js:1737)
at Object.ko.computed.ko.dependentObservable (knockout.js:1946)
at knockout.js:3002
at Object.arrayForEach (knockout.js:151)
at applyBindingsToNodeInternal (knockout.js:2974)
at applyBindingsToNodeAndDescendantsInternal (knockout.js:2854)
at Object.ko.applyBindings (knockout.js:3065)
What is the problem here?
magento-2.1 uicomponent knockoutjs
I have two knockout components. The xml in layout is:
<body>
<referenceContainer name="content">
<block class="VendorModuleBlockList" name="some_name" template="list.phtml">
<arguments>
<argument name="jsLayout" xsi:type="array">
<item name="components" xsi:type="array">
<item name="js_list" xsi:type="array">
<item name="component" xsi:type="string">Vendor_Module/js/js-list</item>
<item name="config" xsi:type="array">
<item name="template" xsi:type="string">Vendor_Module/js-list</item>
</item>
<item name="children" xsi:type="array">
<item name="js_item" xsi:type="array">
<item name="component" xsi:type="string">Vendor_Module/js/js-item</item>
<item name="config" xsi:type="array">
<item name="template" xsi:type="string">Vendor_Module/js-item</item>
</item>
</item>
</item>
</item>
</item>
</argument>
</arguments>
</block>
</referenceContainer>
</body>
The component js_list has a template that contains the fallowing:
<div data-bind="foreach: someArray, visible: someArray().length > 0 ">
<p data-bind="scope: 'js_list.js_item'">
<!-- ko template: getTemplate() --><!-- /ko -->
<p data-bind="text: testVarThatPrintsTest"></p>
</p>
</div>
The content of the template from the component js_item is printed out but when I try to access the variable defined in it testVarThatPrintsTest :
define(
['jquery', 'ko', 'uiComponent', 'Magento_Ui/js/modal/modal', 'domReady!'],
function($, ko, Component, modal) {
'use strict';
return Component.extend({
initialize: function() {
this._super();
},
defaults: {
template: 'Vendor_Module/js-item',
testVarThatPrintsTest: 'TEST TEST TEST@'
}
});
}
);
I get the fallowing error:
Message: testVarThatPrintsTest is not defined
at text (eval at createBindingsStringEvaluator (knockout.js:2624), <anonymous>:3:57)
at update (knockout.js:4260)
at ko.dependentObservable.disposeWhenNodeIsRemoved (knockout.js:3004)
at evaluateImmediate (knockout.js:1737)
at Object.ko.computed.ko.dependentObservable (knockout.js:1946)
at knockout.js:3002
at Object.arrayForEach (knockout.js:151)
at applyBindingsToNodeInternal (knockout.js:2974)
at applyBindingsToNodeAndDescendantsInternal (knockout.js:2854)
at Object.ko.applyBindings (knockout.js:3065)
What is the problem here?
magento-2.1 uicomponent knockoutjs
magento-2.1 uicomponent knockoutjs
edited Nov 13 '17 at 10:42
Lachezar Raychev
asked Nov 13 '17 at 7:57
Lachezar RaychevLachezar Raychev
65331335
65331335
bumped to the homepage by Community♦ 11 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 11 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
remove comma at the end from string testVarThatPrintsTest: 'TEST TEST TEST@',
– Rakesh Jesadiya
Nov 13 '17 at 10:33
that is not the problem
– Lachezar Raychev
Nov 13 '17 at 10:36
add a comment |
remove comma at the end from string testVarThatPrintsTest: 'TEST TEST TEST@',
– Rakesh Jesadiya
Nov 13 '17 at 10:33
that is not the problem
– Lachezar Raychev
Nov 13 '17 at 10:36
remove comma at the end from string testVarThatPrintsTest: 'TEST TEST TEST@',
– Rakesh Jesadiya
Nov 13 '17 at 10:33
remove comma at the end from string testVarThatPrintsTest: 'TEST TEST TEST@',
– Rakesh Jesadiya
Nov 13 '17 at 10:33
that is not the problem
– Lachezar Raychev
Nov 13 '17 at 10:36
that is not the problem
– Lachezar Raychev
Nov 13 '17 at 10:36
add a comment |
1 Answer
1
active
oldest
votes
The problem was that the scope and the binding happened to a element. Probably has something to do with the inline nature of the element and the fact that I was trying to add block elements(from the child component template) to the content of an inline element. When I changed the code to
<div data-bind="foreach: someArray, visible: someArray().length > 0 ">
<div data-bind="scope: 'js_list.js_item'">
<!-- ko template: getTemplate() --><!-- /ko -->
<div data-bind="text: testVarThatPrintsTest"></div>
</div>
</div>
Everything started working just fine.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f201163%2fmagento-2-knockout-component-scope%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem was that the scope and the binding happened to a element. Probably has something to do with the inline nature of the element and the fact that I was trying to add block elements(from the child component template) to the content of an inline element. When I changed the code to
<div data-bind="foreach: someArray, visible: someArray().length > 0 ">
<div data-bind="scope: 'js_list.js_item'">
<!-- ko template: getTemplate() --><!-- /ko -->
<div data-bind="text: testVarThatPrintsTest"></div>
</div>
</div>
Everything started working just fine.
add a comment |
The problem was that the scope and the binding happened to a element. Probably has something to do with the inline nature of the element and the fact that I was trying to add block elements(from the child component template) to the content of an inline element. When I changed the code to
<div data-bind="foreach: someArray, visible: someArray().length > 0 ">
<div data-bind="scope: 'js_list.js_item'">
<!-- ko template: getTemplate() --><!-- /ko -->
<div data-bind="text: testVarThatPrintsTest"></div>
</div>
</div>
Everything started working just fine.
add a comment |
The problem was that the scope and the binding happened to a element. Probably has something to do with the inline nature of the element and the fact that I was trying to add block elements(from the child component template) to the content of an inline element. When I changed the code to
<div data-bind="foreach: someArray, visible: someArray().length > 0 ">
<div data-bind="scope: 'js_list.js_item'">
<!-- ko template: getTemplate() --><!-- /ko -->
<div data-bind="text: testVarThatPrintsTest"></div>
</div>
</div>
Everything started working just fine.
The problem was that the scope and the binding happened to a element. Probably has something to do with the inline nature of the element and the fact that I was trying to add block elements(from the child component template) to the content of an inline element. When I changed the code to
<div data-bind="foreach: someArray, visible: someArray().length > 0 ">
<div data-bind="scope: 'js_list.js_item'">
<!-- ko template: getTemplate() --><!-- /ko -->
<div data-bind="text: testVarThatPrintsTest"></div>
</div>
</div>
Everything started working just fine.
answered Nov 13 '17 at 12:12
Lachezar RaychevLachezar Raychev
65331335
65331335
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f201163%2fmagento-2-knockout-component-scope%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
remove comma at the end from string testVarThatPrintsTest: 'TEST TEST TEST@',
– Rakesh Jesadiya
Nov 13 '17 at 10:33
that is not the problem
– Lachezar Raychev
Nov 13 '17 at 10:36