devdocs icon indicating copy to clipboard operation
devdocs copied to clipboard

Feedback on page: /guides/v2.4/extension-dev-guide/view-models.html

Open audio-engineer opened this issue 5 years ago • 7 comments

General issue

Description:

Line 3 in the "How to write view models" section is wrong. If $viewModel = $block->getViewModel(); is used in the template to reference to the view model class, it won't work when using <argument name="viewModel">.

On the other hand, if $viewModel = $block->getData("viewModel"); is used, <argument name="viewModel" xsi:type="object">OrangeCompany\Catalog\ViewModel\MyNewViewModel</argument> is valid.

Possible solutions:

Line 3 in the example should be replaced with <argument name="view_model" xsi:type="object">OrangeCompany\Catalog\ViewModel\MyNewViewModel</argument>, OR it should be mentioned that $viewModel = $block->getViewModel(); only works in conjunction with <argument name="view_model" xsi:type="object">OrangeCompany\Catalog\ViewModel\MyNewViewModel</argument> and that $viewModel = $block->getData("<argument name>"); should be used if another argument name is used.

Additional information:

Tested on Magento 2.4.1

audio-engineer avatar Jan 09 '21 21:01 audio-engineer

Hi @audio-engineer. Thank you for your report. To help us process this issue please make sure that you provided sufficient information.

Please, add a comment to assign the issue: @magento I am working on this


m2-assistant[bot] avatar Jan 09 '21 21:01 m2-assistant[bot]

General issue

Description:

Line 3 in the "How to write view models" section is wrong. If $viewModel = $block->getViewModel(); is used in the template to reference to the view model class, it won't work when using <argument name="viewModel">.

On the other hand, if $viewModel = $block->getData("viewModel"); is used, <argument name="viewModel" xsi:type="object">OrangeCompany\Catalog\ViewModel\MyNewViewModel</argument> is valid.

Possible solutions:

Line 3 in the example should be replaced with <argument name="view_model" xsi:type="object">OrangeCompany\Catalog\ViewModel\MyNewViewModel</argument>, OR it should be mentioned that $viewModel = $block->getViewModel(); only works in conjunction with <argument name="view_model" xsi:type="object">OrangeCompany\Catalog\ViewModel\MyNewViewModel</argument> and that $viewModel = $block->getData("<argument name>"); should be used if another argument name is used.

Additional information:

Tested on Magento 2.4.1

Issue confirmed!

pawan avatar Jan 10 '21 16:01 pawan

@magento I am working on this

pawan avatar Jan 10 '21 16:01 pawan

@dobooth - Now this PR : #8459 is merged, can we now close this issue as being completed?

BarnyShergold avatar Feb 04 '21 17:02 BarnyShergold

Hello, @BarnyShergold. Yes, I think you can close it now. Good that it's fixed now!

audio-engineer avatar Feb 04 '21 17:02 audio-engineer

Issue closed as completed.

BarnyShergold avatar Feb 04 '21 17:02 BarnyShergold

Fixed by #8459

keharper avatar Feb 04 '21 17:02 keharper