cancel
Showing results for 
Search instead for 
Did you mean: 

How can it use model function out of MVP?

apll
Associate II

Hi!

I want to use a function of the model in a my class out of MVP, is it possible?

void Model::proofPrint()
{
	modelListener->printLabel();
}

printLabel() is a function inside a screen.

I have a class MainApp.cpp that it use how class wrapper for the file DebugConsole.c.

MainApp isn't a screen.

How can i use model.proofPrint in MainApp?

Obviously i call proofPrint when i am in the correct screen.

7 REPLIES 7
cameronf
Senior

If you want direct access to the model instance you can add functionality to the Model that lets it pass out a reference to itself and then use that reference to make calls to the model (like setting a static pointer in its constructor and adding a static getter function for that pointer).

You can also pass messages into the model from you main logic with a queue and have the Model poll that queue at the beginning of its tick method and tie a specific message to calling your printLabel method. I think of this as being cleaner but I could see arguments for wanting direct access to Model in certain cases.

Martin KJELDSEN
Chief III

@Community member​,

When exactly do you want to use the MainApp class? From within your view?

I am in layoutView where it has scrollcontainer and some button, but scrollcontainer has a custom container with many object.

I digit a command with UART and i want call a function inside my custom container but i can't access because the class with UART command isn't a screen so i can't use model function...

Now i resolve using a global variable when i send command UART exact i set a 1 this variable and into model tick i call the currect correct function in the presenter of layoutView.

You can initialize the class with a reference to the presenter, then call a function on there. The presenter knows the model so it can redirect the call.

/Martin

MainApp or labelLayout (custom container)?

LabelView already has reference to Model.

It doesn't really matter which of the two (Custom Container or MainApp class) you pass the reference to - depending on how you're using MyApp internally. The important thing is that neither of them know the model and they can know the model through a presenter reference.

/Martin

How do i make to add a presenter reference?

class LabelLayout : public LabelContainer, public View<LayoutPresenter> {

This generate error of compiling.