Home >Web Front-end >JS Tutorial >Sharing common methods of mockjs
This article mainly shares with you the common methods of mockjs, and explains them with text and code. I hope it can help you.
一.mock.mock()
Generate simulation data based on the data template. More importantly, we must be able to receive this data when we initiate an Ajax request. This is what Mock.mock() does!
Mock.mock( rurl?, rtype?, template|function( options ) )
(1). Parameter meaning and default value
Parameter rurl : Optional. Indicates the URL that needs to be intercepted, which can be a URL string or a URL regular. For example /\/domain\/list.json/, '/domian/list.json'.
Parameter rtype: optional. Indicates the type of Ajax request that needs to be intercepted. For example GET, POST, PUT, DELETE, etc.
Parameter template: optional. Represents a data template, which can be an object or a string. For example { 'data|1-10':[{}] }, '@EMAIL'.
Parameter function(options): optional. Represents a function used to generate response data.
Parameter options: Point to the Ajax option set of this request.
(2).Method
2.Mock.mock(template) Generate simulation data based on the data template
3.Mock.mock(rurl, template)
Record data template. When an Ajax request matching rurl is intercepted, simulated data will be generated based on the data template template and returned as response data.
4.Mock.mock(rurl, function(options))
Record the function used to generate response data. When an Ajax request matching rurl is intercepted, the function function(options) will be executed and the execution result will be returned as response data.
5.Mock.mock(rurl, rtype, template)
Record data template. When an Ajax request matching rurl and rtype is intercepted, simulated data will be generated based on the data template template and returned as response data.
7.Mock.mock(rurl, rtype, function(options))
Record the function used to generate response data. When an Ajax request matching rurl and rtype is intercepted, the function function(options) will be executed and the execution result will be returned as response data.
(3). Liezi
var data8 = Mock.mock(/\.json/,"get",{ "name":"@name", "isTrue|1":true, "color":"@color" }) Mock.mock(/\.json/, 'post', function(options) { return options.type }) $.ajax({ url: 'hello.json', type: 'get', dataType: 'json' }).done(function (data, status, jqXHR) { $('<pre class="brush:php;toolbar:false">').text(JSON.stringify(data, null, 4)) .appendTo('body') }) $.ajax({ url: 'hello.json', type: 'post', dataType: 'json' }).done(function (data, status, jqXHR) { $('<pre class="brush:php;toolbar:false">').text(JSON.stringify(data, null, 4)) .appendTo('body') })
View official website Liezi
二.Mock.Random
Mock.Random is a tool class, used Used to generate various random data
(1). Basically use
1.Mock.Random.boolean() to return a random Boolean value.
2.Random.natural() returns a random natural number (an integer greater than or equal to 0)
3.Random.integer() returns a random integer.
4.Random.float() returns a random floating point number.
5.Random.character() returns a random character.
6.Random.string() returns a random string
7.Random.range(start?, stop, step?) returns an integer array.
View the official website
(2).Date + time
1.Date
Random.date() "2002-10-23"
Random.date('yyyy-MM-dd') // "1975-04-27"
Random.date('yy-MM-dd') // "00-01-08"
2. Time:
Random.time() // "05:06:06"
3. Date and time
Random.datetime('yyyy-MM-dd A HH: mm:ss')
4. Current date and time Ranndom.now()
(3). Simulate Web data
(4). Simulate Geographical location data
(5).Color+text+surname(first name)
Please check the official website
三.Mock.valid(template, data)
Verify whether the real data data matches the data template template.
template Required. Represents a data template, which can be an object or a string. For example { 'list|1-10':[{}] }, '@EMAIL'.
data Required. represents real data.
eg:
var template = { name: 'value1' } var data = { name: 'value2' } Mock.valid(template, data) // => [ { "path": [ "data", "name" ], "type": "value", "actual": "value2", "expected": "value1", "action": "equal to", "message": "[VALUE] Expect ROOT.name'value is equal to value1, but is value2" } ]
4.Mock.toJSONSchema(template)
Convert the Mock.js style data template template into JSON Schema.
5.Mock.setup()
Mock.setup( settings ) Configure the behavior when intercepting Ajax requests. Supported configuration items include: timeout.
The meaning and default value of parameters
Settings Required. Collection of configuration items.
timeout is optional.
Specifies the response time of the intercepted Ajax request, in milliseconds. The value can be a positive integer, such as 400, which means that the response content will be returned after 400 milliseconds; it can also be a hyphen '-' style string, such as '200-600', which means that the response time is between 200 and 600 milliseconds. The default value is '10-100'.
Mock.setup({
timeout: 400
})
Mock.setup({
timeout: '200-600'
})
Currently, the interface Mock.setup(settings) is only used to configure Ajax requests, and may be used to configure other behaviors of Mock in the future.
The above is the detailed content of Sharing common methods of mockjs. For more information, please follow other related articles on the PHP Chinese website!