


Introduction to JavaScript framework (xmlplus) components (7) Routing (ViewStack)
xmlplus is a JavaScriptframework used for rapid development of front-end and back-end projects. This article mainly introduces routing of the xmlplus component design series, which has certain reference value. Interested friends can refer to
. On the browser side, the understanding of routing is general It completes page switching based on different URLs. On the server side, relevant pages are fed back based on different URL requests. In this chapter, we define component routing in a broad sense: component object presents different child pages according to different commands received. Here we will introduce a component related to routing, namely the view stack ViewStack.
View stack preliminary
This component has already appeared in the last chapter of the "Documentation" section, "Delayed Instantiation". Some details will be explained here. The source code of this component is given again below.
ViewStack: { xml: "<p id='viewstack'/>", fun: function (sys, items, opts) { var args, children = this.children(), table = children.call("hide").hash(), ptr = table[opts.index] || children[0]; if (ptr) ptr = ptr.trigger("show").show(); this.on("switch", function ( e, to ) { table = this.children().hash(); if ( !table[to] || table[to] == ptr ) return; e.stopPropagation(); args = [].slice.call(arguments).slice(2); ptr.trigger("hide", [to+''].concat(args)).hide(); ptr = table[to].trigger("show", [ptr+''].concat(args)).show(); }); return Object.defineProperty({}, "selected", { get: function() {return ptr;}}); } }
From the staticinterface, this component allows to provide a static parameter index, which is the name of a child component object of the component ViewStack. It is used to indicate which A child component will be rendered first. See the example below.
Example1: { xml: `<ViewStack index='bar'> <button id='foo'>foo</button> <button id='bar'>bar</button> </ViewStack>` }
In this example, ViewStack contains a attribute index with a value of bar, indicating that when the component is instantiated, the component object bar will be rendered first. By default, the first child component of this component will be used as the initial display object. Looking at the dynamic interface, the component's function item exports a read-only attribute named selected, which is used to indicate the currently displayed child component object.
Switching the target component object through events
For switching between child component objects, the function item of the component does not export the relevant interface, but Switching is completed by receiving the switch event. See the example below.
Example2: { xml: "<ViewStack id='viewstack'>\ <button id='foo'>foo</button>\ <button id='bar'>bar</button>\ </ViewStack>" fun: function (sys, items, opts) { sys.viewstack.on("click", "*", function(e) { var to = this + '' == "foo" ? "bar" : "foo", data = "hello, world"; this.trigger("switch", [to, data]); }); sys.foo.on("show", function (e, prev, data) { console.log("previous page is " + prev, "from data is " + data); }); sys.bar.on("hide", function (e, prev, data) { console.log("previous page is " + prev, "from data is " + data); }); } }
For this example, when the user clicks on the text, the text will switch between foo and bar, that is, switching between the two pages. The switching is performed by dispatching the switch event through the corresponding child object. In addition, when switching pages, the component ViewStack will also dispatch the event show to the page displayed this time, and the event hide to the page hidden this time. The relevant pages can choose to listen or not as needed. And in the listening function, you can get the ID of the previous displayed page and the related data transmitted.
Dynamic addition and removal of child objects
Component ViewStack supports dynamic addition and removal of child component objects, please see an example below.
Example3: { xml: "<ViewStack id='viewstack'>\ <button id='foo'>foo</button>\ </ViewStack>" fun: function (sys, items, opts) { var xml = "<button id='bar'>bar</button>"; sys.viewstack.append(xml).trigger("switch", "bar"); } }
In this example, a child component is dynamically added to the function item, and the currently displayed view is switched to the newly added view by dispatching the switch event.
Optimized configuration
Component ViewStack is generally used in conjunction with the delayed instantiation function of the component. For some more complex components, this can help speed up the display of the application's initial page. Here is a simple demonstration.
Example4: { xml: `<ViewStack> <button id='foo'>foo</button> <button id='bar'>bar</button> <button id='alice'>alice</button> </ViewStack>` map: { defer: "bar alice" } }
In this example, the ViewStack child contains three subcomponents, of which the component objects bar and alice are set to require delayed instantiation. They only really start when the show functions of these two component objects are called. Instantiate.
Here we look at how to make the component ViewStack work with HTML5 Used in conjunction with the History API. Below is a simple example.
Example5: { xml: `<ViewStack id='example'> <button id='foo'>foo</button> <button id='bar'>bar</button> <button id='alice'>alice</button> </ViewStack>`, fun: function (sys, items, opts) { sys.example.on("show", "button", function (e, prev) { window.history.pushState({name: this + ""}, null, "/" + this); }); window.addEventListener("popstate", function(e) { sys.example.trigger("switch", e.state.name); }); } }
The key point of this example is that when the sub-page of the view stack component object changes, use the pushState function to record it; in addition, you need to listen to the popstate event of the browser. When the user clicks "Forward", When pressing the "Back" button, the switching of the corresponding page is completed. This technology is very suitable for completing refresh-free jumps in single-page applications, and can bring a very good experience to users.
This series of articles is based on the xmlplus framework. If you don’t know much about xmlplus, you can visit www.xmlplus.cn. Detailed getting started documentation is available here.
【Related recommendations】
1. Free js online video tutorial
2. JavaScript Chinese Reference Manual
3. php.cn Dugu Jiujian (3) - JavaScript video tutorial
The above is the detailed content of Introduction to JavaScript framework (xmlplus) components (7) Routing (ViewStack). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Many users always encounter some problems when playing some games on win10, such as screen freezes and blurred screens. At this time, we can solve the problem by turning on the directplay function, and the operation method of the function is also Very simple. How to install directplay, the old component of win10 1. Enter "Control Panel" in the search box and open it 2. Select large icons as the viewing method 3. Find "Programs and Features" 4. Click on the left to enable or turn off win functions 5. Select the old version here Just check the box

How to implement API routing in the Slim framework Slim is a lightweight PHP micro-framework that provides a simple and flexible way to build web applications. One of the main features is the implementation of API routing, allowing us to map different requests to corresponding handlers. This article will introduce how to implement API routing in the Slim framework and provide some code examples. First, we need to install the Slim framework. The latest version of Slim can be installed through Composer. Open a terminal and

The default display behavior for components in the Angular framework is not for block-level elements. This design choice promotes encapsulation of component styles and encourages developers to consciously define how each component is displayed. By explicitly setting the CSS property display, the display of Angular components can be fully controlled to achieve the desired layout and responsiveness.

Apache Camel is an Enterprise Service Bus (ESB)-based integration framework that can easily integrate disparate applications, services, and data sources to automate complex business processes. ApacheCamel uses route-based configuration to easily define and manage integration processes. Key features of ApacheCamel include: Flexibility: ApacheCamel can be easily integrated with a variety of applications, services, and data sources. It supports multiple protocols, including HTTP, JMS, SOAP, FTP, etc. Efficiency: ApacheCamel is very efficient, it can handle a large number of messages. It uses an asynchronous messaging mechanism, which improves performance. Expandable

Win10 old version components need to be turned on by users themselves in the settings, because many components are usually closed by default. First we need to enter the settings. The operation is very simple. Just follow the steps below. Where are the win10 old version components? Open 1. Click Start, then click "Win System" 2. Click to enter the Control Panel 3. Then click the program below 4. Click "Enable or turn off Win functions" 5. Here you can choose what you want to open

Vue component development: Progress bar component implementation method Preface: In Web development, the progress bar is a common UI component, often used to display the progress of operations in scenarios such as data requests, file uploads, and form submissions. In Vue.js, we can easily implement a progress bar component by customizing components. This article will introduce an implementation method and provide specific code examples. I hope it will be helpful to Vue.js beginners. Component structure and style First, we need to define the basic structure and style of the progress bar component.

Vue component practice: Introduction to paging component development In web applications, the paging function is an essential component. A good paging component should be simple and clear in presentation, rich in functions, and easy to integrate and use. In this article, we will introduce how to use the Vue.js framework to develop a highly customizable paging component. We will explain in detail how to develop using Vue components through code examples. Technology stack Vue.js2.xJavaScript (ES6) HTML5 and CSS3 development environment

How to use routing to customize page switching animation effects in a Vue project? Introduction: In the Vue project, routing is one of the functions we often use. Switching between pages can be achieved through routing, providing a good user experience. In order to make page switching more vivid, we can achieve it by customizing animation effects. This article will introduce how to use routing to customize the page switching animation effect in the Vue project. Create a Vue project First, we need to create a Vue project. You can use VueCLI to quickly build
