ExtJS Learning: An MVC pattern case

Source: Internet
Author: User

MVC pattern Case (i)

Implementation effect:

As can be seen from the above, the layout of the Web page consists of three parts: the top title, the left menu and the right topic content display. Here we have a preliminary impression of the layout of the case, to facilitate further study of our future.
Looking at the final effect of the project, let's look at the final file structure of the case:

As you can see from the file structure, the app folder is our main working directory throughout the project, including: Controller (Controller), model (data Model), store (DataSet), view (view). There is also the server folder, whose primary purpose is to provide data to the project in lieu of a background server. We'll take a look at the two documents Index.html and App.js today, as we'll step through them in future articles.
index.html File Code:

<!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >  

The main purpose of this document is to refer to the ExtJS framework file, which we do not introduce too much.
App.js File Code:

Ext.onready (function () {
	//Open suspension hint function
	Ext.QuickTips.init ();
	Activate dynamically loaded
	Ext.Loader.setConfig ({
		enabled:true
	});
	Create an instance of the application
	ext.application ({
		//Set namespace
		name: ' Demo ',
		//Specify configuration options, set appropriate path
		Appfolder: ' app ',
		//Load Controller
		controllers: [' Democontroller '],//
		automatically load and instantiate viewport file
		autocreateviewport:true
	});
});

The App.js file can be viewed as a global setting for our entire project, where we need to be aware of:
(1) Ext.Loader.setConfig to help us turn on the automatic loading function, this function is not open by default, we need to manually open, or later developed a lot of files will not find, reported the following error:

(2) Name: ' Demo ' provides a namespace for the project, which will be applied as a global variable throughout the project to help us identify the application files that belong to this project.
(3) Appfolder: ' app ' This configuration item, help us to assign to the app folder.

MVC pattern Case (ii)

How the ExtJS framework uses the MVC pattern to lay out the code.
So far our existing files have only the root directory of the index.html and app.js two files, does not have any practical significance. Let's start by creating our project so that it can actually be shown in our browsers. First, we create the app folder under the root directory, and then create the controller and view two folders under the app folder, storing our controller files and views files separately. Define Viewport

Veiwport represents the entire browser display area, which renders to the body area of the page and automatically changes with the size of the browser display area, with only one viewport instance in a page. Below we create the Viewport.js file in the View folder and add the following code:

Ext.define (' Demo.view.Viewport ', {
	extend: ' Ext.container.Viewport ',
	//Layout way: Border layout
	: ' Border ',
	items: [{
		title: ' ExtJS case ',
		collapisble:true,
		region: ' North ',
		height:100,
		html: ' <br ><center><font Size=5>mvc Mode Implementation of ExtJS case </font><br><font size=2> Source: Itlee Blog </ Font></center> '
	},{
		title: ' Function Menu ',
		region: ' West ',
		width:180,
		split:true,
		Collapisble:true,
		HTML: ' Here is the menu section '
	}, {
		id: ' maincontent ',
		title: ' Topic content display ',
		layout: ' Fit ',
		region: ' Center ',
		collapisble:true,
		contentel: ' Contentiframe '
	}]
;

In viewport we use the border layout to divide the entire page into three parts: Head (North), Zuo (west), Theme section (center). Define a controller

The controller is the key to the entire application, and he is responsible for listening to the events and making the appropriate actions for certain times. Now we create a controller under the Controller folder named Democontroller.js (the name should be the same as the controller that is loaded in the App.js file), and then we add the following code for the file:

Ext.define (' Demo.controller.demoController ', {
	extend: ' Ext.app.Controller ',
        //Add Viewport.js to controller
	Views: [' Viewport ']
});

In the controller we load the desired view into the controller through the views configuration item. Now we can look at the item through the browser and see the effect as follows:

At this point, we are the layout of the Web page is completed, I hope to help you. Next we'll talk about the implementation of the tree menu. MVC pattern Case (iii)

So far, our file structure is shown below:

Next, we add a menttree.js file to the View folder, which is used as a tree menu component. Add the following code for the file:

Ext.define (' Demo.view.menuTree ', {
	extend: ' Ext.tree.Panel ',
	alias: ' Widget.menutree ',
	border:false,
	//Specify the display area of the anchor chain address
	hreftarget: ' maincontent ',//
	whether to display the root node
	rootvisible:false,
	//Data Set
	store: ' Menustore ',
	//Menu style
	bodystyle: {
		background: ' #ffc ',
		padding: ' 10px '
	}
};

So we created a component of the menu, but now our menu doesn't work, because the menu doesn't have the data populated yet. In ExtJS4 we create the data model and dataset using a separate file, and we first create the data model before we create the dataset. Create the Model folder under the App folder, create the Menumodel.js file under the folder, and add the following code to the file:

Ext.define (' Demo.model.menuModel ', {
    extend: ' Ext.data.Model ',
    fields:[
        {name: ' id ', type: ' int '},
        {name: ' pid ', type: ' int '},
        {name: ' text ', type: ' varchar '},
        when//type is a Boolean, subsequent defaults must be written, no error
        {name: ' leaf ', Type: ' Boolean ', defaultvalue:true},
        {name: ' URL ', type: ' varchar '}
    ]
};

With the data model, we then create the store folder and create the Menustore.js file under that folder, adding the following code:

Ext.define ("Demo.store.menuStore", {
	Extend: ' Ext.data.TreeStore ',
	defaultroodid: ' Root ',
	requires: ' Demo.model.menuModel ',
	model: ' Demo.model.menuModel ',
	proxy:{
		type: ' Ajax ',
		URL: './server/ Data.json ',
		Reader: ' json ',
		autoload:true
	}
});

data sets and data models are available, so how do we add data to the menu? In general, the required data for the menu is provided by the backend server, because we mainly explain extjs knowledge, as far as possible not to cover the backend of things, we can use JSON format to simulate the background data output. Now we create the server folder under the root directory, under which a Data.json file is created to provide data for the front end:

[{" id ":" 2 "," pid ":" 1 "," Text ":" User Management "," leaf ":" 0 "," url ":" http:\/\/
		Www.lihuai.net "," children ": [{" id ":" 5 "," pid ":" 2 "," Text ":" Basic Information "," leaf ":" 1 "," url ":" Http:\/\/www.sogou.com ", "Children": "},{" "id": "One", "pid": "2", "Text": "Information Management", "leaf": "1", "url": "Http:\/\/www.sogou.com", "children" : ""},{"id": "", "pid": "2", "Text": "Add User", "leaf": "1", "url": "Http:\/\/www.sogou.com", "Children": ""}]}, {"I D ":" 3 "," pid ":" 1 "," Text ":" Product Management "," leaf ":" 0 "," url ":" Http:\/\/www.so.com "," children ": [{" id ":" 7 "," pid ":" 3 "," "
		Text ":" Product Information "," leaf ":" 1 "," url ":" Http:\/\/www.so.com "," Children ":" "},{" id ":" 8 "," pid ":" 3 "," Text ":" Product Add ", "Leaf": "1", "url": "Http:\/\/www.so.com", "Children": ""}] 

       for simplicity, the URL address for each node is replaced with a simple Web page. Everything is ready, only the difference is loaded. So, how can we load our written menu components into our project?
       First, modify our Viewport.js file to add the menu component to the entire view, with the modified code as follows:

Ext.define (' Demo.view.Viewport ', {
	extend: ' Ext.container.Viewport ',
	//Layout way
	layout: ' Border '
	, Items: [{
		title: ' ExtJS case ',
		collapisble:true,
		region: ' North ',
		height:100,
		html: ' <br> <center><font Size=5>mvc Mode Implementation of ExtJS case </font><br><font size=2> Source: Itlee Blog </font ></center> '
	},{
		title: ' Function Menu ',
		region: ' West ',
		width:180,
		split:true,
		Collapisble:true,
                //Here is the modified part of the
		items:[{
			xtype: ' Menutree '
		}]
	}, {
		id: ' MainContent ',
		title: ' Topic content display ',
		layout: ' Fit ',
		region: ' Center ',
		collapisble:true,
		Contentel: ' Contentiframe '
	}]
});

Next, modify the Democontroller.js file to implement the loading of the menu components, modified code:

Ext.define (' Demo.controller.demoController ', {
	extend: ' Ext.app.Controller ', views
	: [' Viewport ', ' Menutree '],
	stores: [' Menustore '],
	model: [' Menumodel ']
});

Now, with the browser to view our case, the left side has shown the menu bar, the effect of the following figure:

When we clicked on the tree node, we found that the topic part of the right did not show the content of the page, because we had not yet added the reason for the listener event for the node. Specifically how to add the effect of switching pages, we will explain in detail in the next. MVC pattern Case (iv)

We have basically achieved the purpose of the MVC pattern Layout ExtJS project, and we can see the most common page layout structure in the browser. However, as a web developer, it is not possible to achieve a Web page layout is completed tasks, we also need to implement a certain function. In this talk, we will implement when clicked the ExtJS menu node, the page Topic section displays the corresponding content.
So far, our file structure:

We will not add new files, just modify the original file. As we said earlier, the main function of the controller is to monitor the events and control the logic. So, we mainly modify democontroller.js this file today, add the function of switching page for our project. The current code for our Democontroller.js file is:

1 Ext.define (' Demo.controller.demoController ', {
2 Extend: ' Ext.app.Controller '

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.