AD0-E720 practice question 5 of 10
An Adobe Commerce developer wants to apply a knockout binding to ato run a function, onClick(), when it's clicked. Which two solutions would the…
Full set: 50 questionsAn Adobe Commerce developer wants to apply a knockout binding to a
to run a function, onClick(), when it's clicked. Which two solutions would the developer use to achieve this? (Choose two.)
The question requires the correct Knockout.js syntax to bind a click event to a function. Knockout bindings are declared within the data-bind HTML attribute.
Option C, data-bind="click: onClick", is the standard and most direct method. It passes a reference to the onClick function from the view model to the click binding. The function is then executed when the element is clicked.
Option A, data-bind="click: onClick()", is also a valid syntax under specific conditions. The value of a binding is a JavaScript expression. Here, onClick() is executed immediately when bindings are applied, and its return value is used as the click handler. This pattern is valid if the onClick function is designed to return another function that serves as the actual event handler.
B.
This uses containerless (comment-based) syntax, which is reserved for control-flow bindings like if and foreach, not for event bindings on a specific element.
D.
This is not valid Knockout.js syntax. All Knockout bindings must be specified within the data-bind attribute.
1. Knockout.js Official Documentation, 'click' binding: The documentation explicitly shows the primary usage is to provide a function reference (supporting option C). It states, "The function you want to be invoked when the user clicks the element." It also clarifies that the binding's value can be a JavaScript expression, such as a function literal, which supports the technical validity of option A if onClick() returns a function.
Source: Knockout.js Documentation, click binding section. (knockoutjs.com/documentation/click-binding.html)
2. Adobe Commerce Developer Documentation, KnockoutJS integration: Adobe Commerce's documentation provides examples of Knockout.js usage. The prevalent pattern for click events in code examples aligns with option C, demonstrating it as the standard implementation. For instance, in checkout modules, click bindings are typically written as data-bind="click: back".
Source: Adobe Commerce Developer Documentation, Frontend Developer Guide, UI components, KnockoutJS. (developer.adobe.com/commerce/frontend-core/ui-components/knockout-js/)
3. Knockout.js Official Documentation, Containerless control flow: This documentation specifies that the comment-based syntax is intended for bindings like if, ifnot, foreach, with, and template. It does not include event bindings like click, confirming that option B is an incorrect application of the syntax.
Source: Knockout.js Documentation, if binding section, "Note: Using containerless control flow syntax". (knockoutjs.com/documentation/if-binding.html)
Keep practicing
Take the free AD0-E720 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other AD0-E720 practice questions
- Question 1An Adobe Commerce developer is trying to remove a block using the
- Question 3An Adobe Commerce developer wants to completely overwrite _module. less of Orange_Checkou…
- Question 6An Adobe Commerce developer needs to modify the width and height of all product images in…
- Question 8An Adobe Commerce developer wants to remove the default Wishlist and Compare Products blo…
- Question 9An Adobe Commerce developer is customizing buttons for a custom theme that inherits Magen…
- Question 10An Adobe Commerce developer wants to override the following Layout XML file in the theme …
