1

In MVVM (or Presentation Model), my (web) view listens to a CLICK event and calls a function of the Presentation Model to ask for the result.

To clarify, the presentation model lives browser-side. The presentation model then calculates the result asynchronously.

Is it then OK for the Presentation Model to return a (Javascript) promise that the view consumes and updates itself when resolved? (Or is there a reason that I would need to have the view listen to an event from the presentation model and then make a sync call?)

bebbi
  • 361
  • 3
  • 8
  • It sounds like you're asking if there's anything returning a promise cannot do that firing an event later can do. Is that correct? (if so, there can be several different ways for a particular event to get fired, while the promise can only be returned when that particular method is called, so usually only one of the two is what you want) – Ixrec Aug 20 '15 at 09:39
  • I'm mostly concerned about implementing the pattern in a clean way. When I saw my view containing listeners as well as promise.then() constructs, I started to wonder whether I'm doing something wrong.. – bebbi Aug 20 '15 at 09:43
  • 1
    Ah, I see. I'm not familiar enough with MVVM to give a proper answer, but it's probably fine. Promises and events are different mechanisms, and should be used for different things. As long as there's an obvious reason in each case why returning a promise is better or listening to an event is better, then it's fine. For instance, saving a change to a database might involve a promise, since you want to know when *that particular change* is done, while waiting for the user to enter text might involve an event, since you don't care if the user typed it or dragged and dropped it or copy-pasted it. – Ixrec Aug 20 '15 at 09:46

1 Answers1

-1

MVVM is Model-View-ViewModel

  • Model is the server side representation of the data that your application represents
  • View is the presentation layer the UI - HTML
  • ViewModel is a javascript object that contains the models. The view model is a code abstraction of the UI - HTML and shouldn't have any knowlege of the HTML representation, which allows it to be loosely coupled.

The view model interaction is usually handled using javascript binding frameworks such as Angular and Knockout and is pretty much behind the scenes.

Not sure what technologies you are using but here is a nice example which may help you understand the MVVM architecture using a binding framework, in this case knockout.

http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-1

"6. Create the JavaScript Client" I think is more what you're after but I'd recommend going through the entire toutorial.

Heracles
  • 1
  • 1