8

Magento veteran Jessie has a well famous blog on viewModel which you can find it here. It answers your question at the end. It says: When to use ViewModels? When would you want to use ViewModels? Well, when Magento 2.2 comes out and your code doesn't need to be backwards compatible with Magento 2.1 any more, my answer will be: Always! However, ...


7

It is possible and it works as expected! The reason why it was not working was because I was not implementing the right interface in my viewModel class. All viewModels need to to implement ArgumentInterface. namespace Vendor\CustomModule\ViewModel; class Class implements \Magento\Framework\View\Element\Block\ArgumentInterface { public function ...


5

The most important part of using ViewModel is it simplifies the load time as we only implements Magento\Framework\View\Element\Block\ArgumentInterface. If we use a custom block instead of ViewModel, we should extend the core block Magento\Framework\View\Element\Template which includes so many handy things like some extra blocks, loggers etc. And we can ...


2

ViewModel is a replacement of Block in Magento 2. When we create any Block file and we need certain data in the template, we define a function in that Block file. In most cases, we need some custom classes to be injected in the Block constructor to get the data and pass it to the template. Now to do that, we have to forcefully add 2 additional parameters $...


1

You just need a Block if you want to set a template filename dynamically or change something in Block functions like _prepareLayout, apart from that, if your template file is fixed, and you want to get data from some collection or model, you can use ViewModel. These are two great references about it: https://magento.stackexchange.com/a/277954/16416 https:/...


1

As always, it depends ;) I‘ve used the approach you describe, but I prefer to use method arguments instead, e.g. $viewModel->getSomeFoo($block->getProduct()); This decouples the view model from the block. However, if there are many public methods on the view model and many values from the block are required the setting the block on the view model ...


1

ko.observableArray is not much different from ko.observable , if you want to detect and respond to changes on one object, you’d use observables. If you want to detect and respond to changes of a collection of things, use an observableArray. This function is not related to ajax directly in any way, it's use is same as obsevable with difference that it is ...


Only top voted, non community-wiki answers of a minimum length are eligible