Home > Web Front-end > JS Tutorial > body text

Sharing common methods of mockjs

小云云
Release: 2018-03-28 17:02:32
Original
2857 people have browsed it

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) {  
    $(&#39;<pre class="brush:php;toolbar:false">&#39;).text(JSON.stringify(data, null, 4))  
        .appendTo(&#39;body&#39;)  
})  
  
$.ajax({  
    url: &#39;hello.json&#39;,  
    type: &#39;post&#39;,  
    dataType: &#39;json&#39;  
}).done(function (data, status, jqXHR) {  
    $(&#39;<pre class="brush:php;toolbar:false">&#39;).text(JSON.stringify(data, null, 4))  
        .appendTo(&#39;body&#39;)  
})
Copy after login

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: &#39;value1&#39;
}
var data = {
    name: &#39;value2&#39;
}
Mock.valid(template, data)
// =>
[
    {
        "path": [
            "data",
            "name"
        ],
        "type": "value",
        "actual": "value2",
        "expected": "value1",
        "action": "equal to",
        "message": "[VALUE] Expect ROOT.name&#39;value is equal to value1, but is value2"
    }
]
Copy after login

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.

  1. 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!