This example demonstrates how to add a toolbar (toolbar) to the data grid (datagrid).
Create a data grid (DataGrid)
url="data/datagrid_data.json"
title="DataGrid with Toolbar" iconCls="icon-save"
toolbar="#tb">
Node.js is a single-process single-threaded applications, but due to the asynchronous callback interfaces V8 engine provides, can handle a large number of concurrent Through these interfaces, so performance is very high.
Node.js API is supported by almost every callback function.
Node.js substantially all of the event mechanism is implemented in design mode observer mode.
Node.js is similar to entering a single thread while (true) event loop until there are no exit events observer, each asynchronous event generates an event observer, if an event occurs is called the callback function.
Event-driven programming
Node.js event-driven model, when the web server receives the request, and then processed to put it off, and then the next request to a web service.
When the request is completed, it is placed back processing queue, when the queue reaches the beginning, the result is returned to the user.
This model is very efficient scalability is very strong, because the webserver been receiving requests without waiting for any read or write operation. (This is also known as event-driven, non-blocking IO or IO)
In the event-driven model, it will generate a main loop to monitor events, triggers a callback function when an event is detected.
The entire event-driven process is so achievable, very simple. Somewhat similar to the observer pattern, the event is equivalent to a theme (Subject), and the processing functions on this event is equivalent to all registered observers (Observer).
Node.js has multiple built-in event, we can through the introduction of events module, and to bind and listen for events by instantiating EventEmitter class, the following examples:
// introduction events module
var events = require('events');
// Create an object eventEmitter
var eventEmitter = new events.EventEmitter();
The following procedures binding event handler:
// Bind event handlers and events
eventEmitter.on('eventName', eventHandler);
We can be triggered by events program:
// trigger event
eventEmitter.emit('eventName');
Examples
Creating main.js file, the code is as follows:
// introduction events module
var events = require('events');
// Create an object eventEmitter
var eventEmitter = new events.EventEmitter();
// create an event handler
var connectHandler = function connected() {
the console.log ( 'the connection is successful.');
// trigger event data_received
eventEmitter.emit('data_received');
}
// Bind connection event handler
eventEmitter.on('connection', connectHandler);
// use anonymous functions bound data_received event
eventEmitter.on('data_received', function(){
the console.log ( 'received the data successfully.');
});
// trigger connection event
eventEmitter.emit('connection');
console.log ( "program is finished.");
Let's execute the code above:
$ node main.js
connection succeeded.
Data reception was successful.
Program execution is completed.
How Node application works?
In Node application function performs the asynchronous operation as the last parameter callback, the callback function receives an error object as the first parameter.
Let us re-look at the previous examples, create a input.txt, document reads as follows:
Agriculture tutorial code official website address: www.codercto.com
Creating main.js file, the code is as follows:
var fs = require("fs");
fs.readFile('input.txt', function (err, data) {
if (err){
console.log(err.stack);
return;
}
console.log(data.toString());
});
console.log ( "program is finished");
Program above fs.readFile () function to read a file is asynchronous. If an error occurs in the process of reading the file, the error err objects will output an error message.
If no error occurs, readFile skip output err object, the document is output by the callback function.
The above code is executed, execution results are as follows:
Program is finished
Agriculture tutorial code official website address: www.codercto.com
Next we delete input.txt file, the results are as follows:
Program is finished
Error: ENOENT, open 'input.txt'
Input.txt because the file does not exist, so the output of the error message. (Editor: Leilin Peng Source: network intrusion deleted)
Item ID | Product ID | List Price | Unit Cost | Attribute | status was based |
---|
We do not need to write any javascript code simply by 'toolbar' property attached toolbar (toolbar) to the data grid (datagrid).
Download jQuery EasyUI examples
jeasyui-datagrid-datagrid4.zip (edit: Leilin Peng Source: network intrusion deleted)