Git Product home page Git Product logo

Comments (2)

bobspryn avatar bobspryn commented on April 28, 2024

Sorry if this is a bit in the weeds, but I just like to have a thorough grasp of patterns and their why's and why-not's.

To provide a current example:

I have a view controller that displays a post. That view controller has a view model. One of the cells displays hash tags associated with that post. The hash tags are tappable, and ultimately should cause a new view controller to be pushed onto the stack with data about that hashtag.

So I have a custom cell, with the ability to return hot words tapped in a label. Somehow I need to surface this action of tapping a hashtag, and the respective hashtag name, up to the primary view model so that it can create a view model for that hashtag detail page and supply it to the view controller.

I've went through several incarnations of this:

  1. The cell having it's parent view controller's view model. This is easy as I can just call a method on that view model, but it's coupling things together that need not know of each other. Less reusable.
  2. The cell has no view model, and I propagate events to the view controller either via the responder chain, or signals exposed on the cell. (The one nice thing about the responder chain in this instance is not setting up new bindings every time a cell with signals was re-used, though you would have to look at the cell's .m code to determine what selectors to respond to). Instead of actually implementing the selector, I used signalForSelector.
  3. The cell has it's own view model, created from the parent view model and assigned in the view controller when preparing the cell. In this case the cell propagates events to it's view model via commands. The parent view model can then observe the signals on those commands and take the appropriate action. This has the downside of creating another file for a simple cell, and I'm not sure it makes sense for a view model to be more or less propagating/broadcasting ui events like that which it doesn't really care about.

from reactiveviewmodel.

joshaber avatar joshaber commented on April 28, 2024

I think both (2) and (3) are legitimate ways of solving the problem. If the cell is simple enough that it doesn't need its own view model, (2) seems perfectly fine.

from reactiveviewmodel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.