"JS Basic" "Source learning" backbone source reading (three)

Source: Internet
Author: User

Recently read the Backbone.js source code, here for the details of the source code is no longer detailed, we can star my source reading project (Https://github.com/JiayiLi/source-code-study) for reference Exchange, Have detailed source code comment, as well as knowledge summary, at the same time Google a bit backbone source code, also has a lot of excellent articles can be used to study.

I am here to record some of the biased design direction of knowledge points. This article is mainly about Rest and CRUD used in Backbone.sync.

First, let's take a quick look at REST:

REST : Representational State Transfer, which is the representational status transfer, is an architectural design style, one of the Web service implementations, designed to facilitate the transmission of information between different software/programs in the network (e.g. the Internet).

The following 6 points are required for this design style:
A, client-server (Client-server)
Communication can only be initiated unilaterally by the client, in the form of a client request-server response.
B, stateless (stateless)
The session state of the communication should all be maintained by the client.
C, caching (cache)
The response content can be cached somewhere in the communication chain to improve network efficiency.
D, unified interface (Uniform Interface)
The components of a communication chain communicate with each other through a unified interface to improve the visibility of the interaction.
E, tiered systems (layered system)
By restricting the behavior of the component (that is, each component can only "see" the next layer it interacts with), the schema is decomposed into layers of several tiers.
F, on-demand code (Code-on-demand, optional)
Support extends the functionality of the client by downloading and executing some code, such as Java applets, Flash, or JavaScript.

The above from the wiki (Https://zh.wikipedia.org/wiki/REST), it looks more difficult to understand.

We understand the application of the Sync module in backbone, which uses the REST design-based HTTP service to perform CRUD operations on Backbone's model (responsible data module) in the Sync module.
The so-called HTTP service, which only does two things: Handling HTTP requests and sending HTTP responses.
And for REST itself is a design style, he tells us how to use the HTTP protocol, focusing on the design of the interface API.
When we design an interface that satisfies the REST constraints and principles, it is called a restful API (restful equals an adjective).

Let's take two APIs that we often touch and HTTP requests (GET, POST):
1. Get Example: Blog Park get my blog page ("My Blog" link address: http://www.cnblogs.com/lijiayi/)

Take a look at the request message that the browser initiated to the server:

The focus is on the first line

get/lijiayi/http/1.1

Where get: Represents the requested method, as well as the most commonly used POST and DELETE, put, and so on. /lijiayi/: Request path. http/1.1: Protocol and version. 2. POST Example: Blog Park compose new short message, submit form

Take a look at the request message that the browser initiated to the server:

The focus is on the first line

Post/ajax/msg/send http/1.1


Same
POST: Represents the requested method.
/ajax/msg/send: Request path.
http/1.1: Protocol and version.

The above is our most common request method, non-RESTful API.

And in Backbone's sync, let's ask for the interface:

Backbone.sync (method, model, [options])

Sync accepts three parameters, where the method parameter is passed into the CRUD method i.e. "create", "read", "Update", "delete", and "patch", while they correspond to the HTTP request method "POST", "GET", "PUT", " DELETE "and" PATCH ".

You can see that we've used most of the HTTP request methods (as well as connect, options, etc.) that are interesting to know: http://www.runoob.com/http/http-methods.html. For different actions for the user, the corresponding request method is different. For the requested URL, we would define this:
/questions  //View all Issues List /questions/:id  // operation ID is an ID issue

Only the above two routes, we can see all the problems, and one of the operations, this operation includes adding and removing changes, for what we specifically do, is the above method is the different HTTP methods to control. This API is designed to be compliant with RESTful APIs.

You probably have a meeting, we are talking about the system:
A restful API is called the Web API that fits the rest design style. It requires that we have:
1, intuitive short request address: for example: http://example.com/resources/.
2, the expression of the resources we request can be XML, HTML or JSON and so on.
3, the operation of the resources: using the HTTP method to achieve, or expression (such as: Post,get,put or delete).

This means using URLs to locate resources, using HTTP verbs (get,post,delete,put) to describe operations. (from https://www.zhihu.com/question/28557115/answer/41265890)
Usually:

Url GET POST DELETE PUT

A group of resources:
http://example.com/resources/

Get all the resource details in this resource group Create/Append a new resource in this group of resources Delete a resource group Replaces the current resource group with a given set of resources

Individual resources:
Http://example.com/resources/12

Gets the details of the specified resource

Treat the specified resource as a resource group,

And under which a new element is created/appended to the current resource

To delete a specific resource in a resource group

Replace/create the specified resource,

and add it to the appropriate resource group

Above. Thanks and learn from: https://stackoverflow.com/questions/18504235/understand-backbone-js-rest-callshttps://zh.wikipedia.org/ Wiki/rest Recommendation: https://www.zhihu.com/question/28557115 Many people's answer is very good also very deep, have wanted to know more about the classmate, recommend reading.

"JS Basic" "Source learning" backbone source reading (three)

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.