How to Use the PureMVC framework of javascript-Command

Source: Internet
Author: User

The Controller saves the Command ing of all commands. The Command class is stateless and is created only when needed.

Command is responsible for processing complex data logic. Its use is summarized in two ways.

1. trigger Command by sending Notification

2. addSubCommand () to start

 

Let's get started right away.

 

Create a controller folder under the demo folder, and create a js file named StartupCommand. js under the controller folder. Enter the following content

[Javascript]
Var StartupCommand = Objs ("sweeps. controller. StartupCommand ",
MacroCommand,
{
/**
* @ Override
* Add Subcommands to start the PuerMVC component.
* Generally, the Model (Proxy) and View (Mediators) are prepared first );
*/
Initializemedia command: function (note)
{
Alert ('Received from facade ');
// This. addSubCommand (PrepModelCommand );
// This. addSubCommand (PrepViewCommand );
}

});
Create a folder abc under the demo folder to store Noticefiction, create the NotificationNames. js file under the abc folder, and enter the following content

[Javascript]
Var icationicationnames = Objs ("demo. abc. NotificationNames ",{});
 
Icationicationnames. STARTUP = "startup ";
Import the StartupCommand. js and icationicationnames. js files in the html file.

[Html] view plaincopyprint?
<Script type = "text/javascript" src = "src/demo/abc/icationicationnames. js"> </script>
<Script type = "text/javascript" src = "src/demo/controller/StartupCommand. js"> </script>
After completion, the directory tree of the program is


Return to how to use the PureMVC framework of javascript last time-initialize the ApplicationFacade. js class in it

Add code in the initializeController () method

[Javascript]
<Pre name = "code" class = "javascript"> ApplicationFacade. $ super. initializeController. call (this );
// Register the StartupCommand and have it listen to the STARTUP message
This. registerCommand (icationicationnames. STARTUP, StartupCommand );
And add code in the startup () method.
[Javascript] view plaincopyprint?
// Send the STARTUP message to StartupCommand
This. sendNotification (icationicationnames. STARTUP, app );
The StartupCommand class is triggered by sending the Notification using sendNotification (notice, app.

The program calls the initializeController () method during initialization, and registers the StartupCommand instance using the registerCommand () method,

In this way, when the NotificationNames. STARTUP message sent by sendNotification () method is received, the initializemedia command () method of the StartupCommand instance will be triggered.
After running, you will see "received from facade ".


Next let's take a look at how to use addSubCommand () to start the Command instance.

Create the PrepViewCommand. js file in the controller folder and enter the following content:

[Javascript]
Var PrepViewCommand = Objs ("demo. controller. PrepViewCommand ",
SimpleCommand,
{
/**
* @ Override
*/
Execute: function (note)
{
Alert ('use addSubCommand to call Command instance ');
}
}); Www.2cto.com
Then import the PrepViewCommand. js file into the html file.

[Javascript]
<Script type = "text/javascript" src = "src/demo/controller/PrepViewCommand. js"> </script>
Add the following content in the initializemedia command method of the StartupCommand. js file:

[Javascript]
This. addSubCommand (PrepViewCommand );
In this way, you can call the execut () method of the PrepViewCommand. js file!

Is there output?


The directory tree of the last folder is

 
Author: laogong5i0

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.