View Model in Magento 2 | Mage Mastery

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ก.พ. 2025

ความคิดเห็น • 15

  • @AlessandroRonchi
    @AlessandroRonchi 5 ปีที่แล้ว +1

    Crystal clear explanation, I loved it!

  • @balaji-venkatraman
    @balaji-venkatraman 3 ปีที่แล้ว +2

    very tough to understand for beginners. The blog and video content not matching. Less quality when comparing to other videos. Very confused.

    • @mosesegboh
      @mosesegboh 3 ปีที่แล้ว

      exactly, didn't understand anything

    • @MaxPronko
      @MaxPronko  3 ปีที่แล้ว

      What questions do you have?

  • @mohamedmekkaoui3277
    @mohamedmekkaoui3277 5 ปีที่แล้ว +1

    so did I understand it when I say : before we had blocks to implement calculation logic, data lookups etc... but since these can be used in multiple templates, but we don't want those templates to have the same block (because of different needs), we put only the duplicated code in a dedicated VM class (ex: a product VM can be used in minicart and in catalog templates, but those two areas are different blocks) ?
    Thank you for the video btw :)

    • @MaxPronko
      @MaxPronko  5 ปีที่แล้ว +1

      Blocks shouldn’t be used as this is an old approach where inheritance of classes is used. It leads to more errors and backward incompatible changes. Use View Models in all cases.

  • @pegatino
    @pegatino 5 ปีที่แล้ว +2

    This example it confuses me, is not so practical as the previous lessons. Where does the word "friend" come from?

    • @MaxPronko
      @MaxPronko  5 ปีที่แล้ว

      I didn’t understand your question. What exactly confuses you?

    • @danieldespain
      @danieldespain 4 ปีที่แล้ว +3

      I agree. In this lesson, unlike previous ones, there are just unrelated code snippets with no context. Additionally, the html page examples don't match the video examples. For example, is it like the video, or is it like the page? If so, where did go? The class has a required argument on __construct, but then none is sent? And the function getFirstName calls getFirstName? One place it's users, then then it's SKUs. Sometimes you use ViewModel and sometimes it's view_model. Is that a typo? or is that how it works? The other lessons have been really awesome, but this one is very hard to implement. If the source code was available that would help greatly.

  • @igorocheretnyy317
    @igorocheretnyy317 4 ปีที่แล้ว +1

    Hi Max, conceptual difference still now clear why we should use maxBlock instead of maxViewModel. Reason not clear why use maxViewModel.
    You said maxViewModel used to simplify layout and block classes - I guess it's total not clear.
    1. class maxBlock extends \Magento\Framework\View\Element\Template
    2. class maxViewModel implements \Magento\Framework\View\Element\Block\ArgumentInterface
    Both classes represents data.
    At layout xml you can write:

    maxViewModel

    • @MaxPronko
      @MaxPronko  4 ปีที่แล้ว

      Igor, with a view model class we don’t have a legacy inheritance and dependencies as we only implement an ArgumentInterface. This should be enough to use view models over Blocks.
      When using view models, you depend only on classes you need, rather than parent class dependencies like we have in a Template class.
      So in layout xml file, we mo longer require to provide a name of any block if we use a view model that provides access to data.
      Your example uses both, but in reality you don’t need to use Block at all, as a view model gives you all data you might want to render.

    • @igorocheretnyy317
      @igorocheretnyy317 4 ปีที่แล้ว

      @@MaxPronko thx

  • @artashesbaghdasaryan4948
    @artashesbaghdasaryan4948 4 ปีที่แล้ว

    Can we use View Models for widgets too ?

    • @MaxPronko
      @MaxPronko  4 ปีที่แล้ว +1

      You can, for this you should refer a view model class for the widget in order to access it in a template

  • @timeforchangethings
    @timeforchangethings 5 ปีที่แล้ว +2

    Good