IOC Implementation of Newland. js

Source: Internet
Author: User

The most important reason why Newland. JS is a framework rather than a class library is that it can build a website in just a few steps! In other words, it already contains an important process from website startup to running. As for what else you need, whether it is formulated or improved, and so on, the framework has reserved a location for you to fill in the blanks.

FrameworkProgramIt is irreversible to turn a worker into a worker on the assembly line. The only difference is that the assembly line is very intelligent, so that you can have a pleasant life, and some are very disgusting, turn you into a Cool cool!

One of the three major Java frameworks, spring's greatest initiative is to invent the IOC container. Yes, the framework has done a lot for you, but some files still need to be created under the specified directory; someCodeYou still need to write, according to the prescribed format, which class is to be created, and which parent class is to be inherited, which is not a step away. The framework brings both convenience and shackles! The objective of IOC is to reduce these pains!

First, let's copy a paragraph to explain what is IOC --"In dependency injection mode, the creation of the called instance is no longer completed by the caller. Therefore, it is called control inversion. The creation of the called instance is usually completed by the container and then injected to the caller, therefore, it is also called dependency injection.".

In my Newland. in the JS framework, the initial mass. in fact, the JS module only provides the module loading mechanism and some common methods. The only thing that makes sense is that it calls the MVC module.

 
$. Require ("system/deploy, system/MVC", function (deploy) {deploy (process. CWD (); // listen to file changes in the app directory for hot start });

The role of the MVC module is to start the tablet and accept the request! Before that, it reads the configuration file, reads the user-specified interceptor and default interceptor and all controllers into the memory, and assembles them into the system!

The so-called bar interceptor is actually the structs set of things. If you do not know Java, you can understand node. JS, let's look at the middleware of connectjs, which is similar to that, used to process requests and responses.

The Controller is the focus, and MVC cannot leave it alone. A controller has many functions, so we extract shared functions to build a base class, and the Controller we build can save a lot of code by inheriting it as a subclass.

 
// Traverse all the Controller modules under the APP/controllers directory and load them together with the blocker module! $. Walk ("app/controllers", function (Files) {$. require (Inter. concat (files), function () {// The Interceptor is placed at the beginning of VaR intercepters = []. slice. call (arguments, 0, Inter. length); resource_ready (intercepters )});});

Before IOC is used, our controller looks like this:

. Define ("doc_controller", function () {var Klass = $. factory ({inherit: $. base_controller, index: function (flow) {$. log ("already in Doc # index action"); flow. session. set ("XXX", "in Doc") var view_url = $. path. join ("app", "views", flow. req. URL); flow. fire ("get_view", view_url, flow. req. URL)}); $. controllers ["Doc"] = new Klass });

In fact, the user needs to write an action method like index, show, edit, and destroy. The other part is the dead format! If you specify a parent class, the new instance is actually handed over to the system for processing! So our custom controller can be reduced to this!

 
$. Define ("doc_controller", function () {return {index: function (flow) {$. log ("already in Doc # index action"); flow. session. set ("XXX", "in Doc") var view_url = $. path. join ("app", "views", flow. req. URL); flow. fire ("get_view", view_url, flow. req. URL )}}});

specify the subclass creation and inheritance relationships and create instances, and upload them to MVC. js for management:

VaR rcname = // (\ W +) _ controller/; // traverse all the Controller modules in the app/controllers directory and load them together with the blocker module! $. Walk ("app/controllers", function (Files) {$. require (Inter. concat (files), function () {// get the interceptor var intercepters = []. splice. call (arguments, 0, Inter. length); // obtain the Controller var controllers = arguments placed behind it; // reverse the control to construct the Controller subclass and its instance files. foreach (function (El, I) {// The path in Mac is APP/controllers/doc_controller.js // The path in window is app \ controllers \ doc_controller.js var match = El. replace (// \/g ,"/"). match (rcname); var controller = controllers [I]; controller. inherit = $. base_controller var Klass = $. factory (Controller); $. controllers [Match [1] = new Klass;}); resource_ready (intercepters )});});

It should be noted that Newland. JS is divided into two parts. The modules in the app directory are the modules (hereinafter referred to as the app module) created by the user's self-built files or system commands ), the modules in the system directory are used to help the app module run. The simpler the app module, the better! It is best not to use any complex classes, even if they are processed by the framework and new instances are used. Any complicated functions are implemented in the form of hooks, or in other words, it is the form of a callback function, which is scheduled by the Framework. All uncertain functions are made into the form of custom events, and the modules loaded by the framework or framework go to the fire. In this case, all coupling is relaxed to adapt to changes in demand to the maximum extent.

Http://dotnetfresh.cnblogs.com/archive/2005/06/27/181878.html

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.