backbone.Router History源码笔记
Backbone.History和Backbone.Router
history和router都是控制路由的,做一个单页应用,要控制前进后退,就可以用到他们了。
History类用于监听URL的变化,和触发Action方法,他可以添加对url的监听,
Router类用于定义和解析路由规则,并将URL映射到Action。
router和history一些个人的注解
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8"/>
- <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
- <title>backbone</title>
- <style type="text/css">
- *{padding:0;margin:0;}
- .wrap{width:960px; margin: 100px auto; padding: 20px 0;}
- ul{ list-style: none;}
- </style>
- </head>
- <body>
- <div class="wrap">
- <div id="a1"></div>
- <div id="a2"></div>
- <div id="a3"></div>
- </div>
- <script src="http://files.cnblogs.com/wtcsy/jquery.js"></script>
- <script src="http://files.cnblogs.com/wtcsy/underscore.js"></script>
- <script src="http://files.cnblogs.com/wtcsy/events.js"></script>
- <script>
- (function(){
- // Backbone.History
- // ----------------
- // Cached regex for stripping a leading hash/slash and trailing space.
- var routeStripper = /^[#\/]|\s+$/g;
- // Cached regex for stripping leading and trailing slashes.
- var rootStripper = /^\/+|\/+$/g;
- // Cached regex for stripping urls of hash.
- var pathStripper = /#.*$/;
- // Handles cross-browser history management, based on either
- // [pushState](http://diveintohtml5.info/history.html) and real URLs, or
- // [onhashchange](https://developer.mozilla.org/en-US/docs/DOM/window.onhashchange)
- // and URL fragments. If the browser supports neither (old IE, natch),
- // falls back to polling.
- var History = Backbone.History = function() {
- this.handlers = [];
- _.bindAll(this, 'checkUrl');
- // Ensure that `History` can be used outside of the browser.
- if (typeof window !== 'undefined') {
- this.location = window.location;
- this.history = window.history;
- }
- };
- // Has the history handling already been started?
- History.started = false;
- _.extend(History.prototype, Backbone.Events, {
- // The default interval to poll for hash changes, if necessary, is
- // twenty times a second.
- interval: 50,
- // Are we at the app root?
- atRoot: function() {
- var path = this.location.pathname.replace(/[^\/]$/, '$&/');
- return path === this.root && !this.location.search;
- },
- // Gets the true hash value. Cannot use location.hash directly due to bug
- // in Firefox where location.hash will always be decoded.
- getHash: function(window) {
- var match = (window || this).location.href.match(/#(.*)$/);
- return match ? match[1] : '';
- },
- // Get the pathname and search params, without the root.
- getPath: function() {
- var path = decodeURI(this.location.pathname + this.location.search);
- var root = this.root.slice(0, -1);
- if (!path.indexOf(root)) path = path.slice(root.length);
- return path.slice(1);
- },
- // Get the cross-browser normalized URL fragment from the path or hash.
- getFragment: function(fragment) {
- if (fragment == null) {
- if (this._hasPushState || !this._wantsHashChange) {
- fragment = this.getPath();
- } else {
- fragment = this.getHash();
- }
- }
- //var routeStripper = /^[#\/]|\s+$/g;
- return fragment.replace(routeStripper, '');
- },
- // Start the hash change handling, returning `true` if the current URL matches
- // an existing route, and `false` otherwise.
- start: function(options) {
- if (History.started) throw new Error("Backbone.history has already been started");
- History.started = true;
- // Figure out the initial configuration. Do we need an iframe?
- // Is pushState desired ... is it available?
- this.options = _.extend({root: '/'}, this.options, options);
- this.root = this.options.root;
- this._wantsHashChange = this.options.hashChange !== false;
- this._hasHashChange = 'onhashchange' in window;
- this._wantsPushState = !!this.options.pushState;
- this._hasPushState = !!(this.options.pushState && this.history && this.history.pushState);
- this.fragment = this.getFragment();
- // Add a cross-platform `addEventListener` shim for older browsers.
- var addEventListener = window.addEventListener || function (eventName, listener) {
- return attachEvent('on' + eventName, listener);
- };
- // Normalize root to always include a leading and trailing slash.
- // var routeStripper = /^[#\/]|\s+$/g;
- this.root = ('/' + this.root + '/').replace(rootStripper, '/');
- // Proxy an iframe to handle location events if the browser doesn't
- // support the `hashchange` event, HTML5 history, or the user wants
- // `hashChange` but not `pushState`.
- if (!this._hasHashChange && this._wantsHashChange && (!this._wantsPushState || !this._hasPushState)) {
- var iframe = document.createElement('iframe');
- iframe.src = 'javascript:0';
- iframe.style.display = 'none';
- iframe.tabIndex = -1;
- var body = document.body;
- // Using `appendChild` will throw on IE < 9 if the document is not ready.
- this.iframe = body.insertBefore(iframe, body.firstChild).contentWindow;
- this.navigate(this.fragment);
- }
- // Depending on whether we're using pushState or hashes, and whether
- // 'onhashchange' is supported, determine how we check the URL state.
- if (this._hasPushState) {
- addEventListener('popstate', this.checkUrl, false);
- } else if (this._wantsHashChange && this._hasHashChange && !this.iframe) {
- addEventListener('hashchange', this.checkUrl, false);
- } else if (this._wantsHashChange) {
- this._checkUrlInterval = setInterval(this.checkUrl, this.interval);
- }
- },
- // Add a route to be tested when the fragment changes. Routes added later
- // may override previous routes.
- route: function(route, callback) {
- this.handlers.unshift({route: route, callback: callback});
- },
- // Checks the current URL to see if it has changed, and if it has,
- // calls `loadUrl`, normalizing across the hidden iframe.
- checkUrl: function(e) {
- var current = this.getFragment();
- if (current === this.fragment && this.iframe) {
- current = this.getHash(this.iframe);
- }
- if (current === this.fragment) return false;
- if (this.iframe) this.navigate(current);
- this.loadUrl();
- },
- // Attempt to load the current URL fragment. If a route succeeds with a
- // match, returns `true`. If no defined routes matches the fragment,
- // returns `false`.
- loadUrl: function(fragment) {
- fragment = this.fragment = this.getFragment(fragment);
- return _.any(this.handlers, function(handler) {
- if (handler.route.test(fragment)) {
- handler.callback(fragment);
- return true;
- }
- });
- },
- // Save a fragment into the hash history, or replace the URL state if the
- // 'replace' option is passed. You are responsible for properly URL-encoding
- // the fragment in advance.
- //
- // The options object can contain `trigger: true` if you wish to have the
- // route callback be fired (not usually desirable), or `replace: true`, if
- // you wish to modify the current URL without adding an entry to the history.
- navigate: function(fragment, options) {
- if (!History.started) return false;
- if (!options || options === true) options = {trigger: !!options};
- var url = this.root + (fragment = this.getFragment(fragment || ''));
- // Strip the hash for matching.
- // var pathStripper = /#.*$/;
- fragment = fragment.replace(pathStripper, '');
- if (this.fragment === fragment) return;
- this.fragment = fragment;
- // Don't include a trailing slash on the root.
- if (fragment === '' && url !== '/') url = url.slice(0, -1);
- // If pushState is available, we use it to set the fragment as a real URL.
- if (this._hasPushState) {
- this.history[options.replace ? 'replaceState' : 'pushState']({}, document.title, url);
- // If hash changes haven't been explicitly disabled, update the hash
- // fragment to store history.
- } else if (this._wantsHashChange) {
- this._updateHash(this.location, fragment, options.replace);
- if (this.iframe && (fragment !== this.getHash(this.iframe))) {
- // Opening and closing the iframe tricks IE7 and earlier to push a
- // history entry on hash-tag change. When replace is true, we don't
- // want this.
- if(!options.replace) this.iframe.document.open().close();
- this._updateHash(this.iframe.location, fragment, options.replace);
- }
- // If you've told us that you explicitly don't want fallback hashchange-
- // based history, then `navigate` becomes a page refresh.
- } else {
- return this.location.assign(url);
- }
- if (options.trigger) return this.loadUrl(fragment);
- },
- // Update the hash location, either replacing the current entry, or adding
- // a new one to the browser history.
- _updateHash: function(location, fragment, replace) {
- if (replace) {
- var href = location.href.replace(/(javascript:|#).*$/, '');
- location.replace(href + '#' + fragment);
- } else {
- // Some browsers require that `hash` contains a leading #.
- location.hash = '#' + fragment;
- }
- }
- });
- Backbone.history = new History;
- //Backbone.history.start()
- //Backbone.history.navigate
- // Backbone.Router
- // ---------------
- // Routers map faux-URLs to actions, and fire events when routes are
- // matched. Creating a new one sets its `routes` hash, if not set statically.
- var Router = Backbone.Router = function(options) {
- options || (options = {});
- if (options.routes) this.routes = options.routes;
- this._bindRoutes();
- this.initialize.apply(this, arguments);
- };
- // Cached regular expressions for matching named param parts and splatted
- // parts of route strings.
- var optionalParam = /\((.*?)\)/g;
- var namedParam = /(\(\?)?:\w+/g;
- var splatParam = /\*\w+/g;
- var escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g;
- // Set up all inheritable **Backbone.Router** properties and methods.
- _.extend(Router.prototype, Backbone.Events, {
- // Initialize is an empty function by default. Override it with your own
- // initialization logic.
- initialize: function(){},
- // Manually bind a single named route to a callback. For example:
- //
- // this.route('search/:query/p:num', 'search', function(query, num) {
- // ...
- // });
- //
- route: function(route, name, callback) {
- if (!_.isRegExp(route)) route = this._routeToRegExp(route);
- if (_.isFunction(name)) {
- callback = name;
- name = '';
- }
- if (!callback) callback = this[name];
- var router = this;
- Backbone.history.route(route, function(fragment) {
- var args = router._extractParameters(route, fragment);
- if (router.execute(callback, args, name) !== false) {
- router.trigger.apply(router, ['route:' + name].concat(args));
- router.trigger('route', name, args);
- Backbone.history.trigger('route', router, name, args);
- }
- });
- return this;
- },
- // Convert a route string into a regular expression, suitable for matching
- // against the current location hash.
- _routeToRegExp: function(route) {
- route = route.replace(escapeRegExp, '\\$&') //把正则里面需要转移的字符进行转移
- .replace(optionalParam, '(?:$1)?') //把捕获变成非捕获 并且变成惰性匹配
- .replace(namedParam, function(match, optional) {
- return optional ? match : '([^/?]+)';
- }) //如果是:\w+格式转化成([^/?]+) 如果是非捕获格式(?: 则不进行转换
- .replace(splatParam, '([^?]*?)'); //把这种*\w+格式替换成 ([^?]*?)
- return new RegExp('^' + route + '(?:\\?([\\s\\S]*))?$');
- },
- // Simple proxy to `Backbone.history` to save a fragment into the history.
- navigate: function(fragment, options) {
- Backbone.history.navigate(fragment, options);
- return this;
- },
- // Execute a route handler with the provided parameters. This is an
- // excellent place to do pre-route setup or post-route cleanup.
- execute: function(callback, args, name) {
- if (callback) callback.apply(this, args);
- },
- // Bind all defined routes to `Backbone.history`. We have to reverse the
- // order of the routes here to support behavior where the most general
- // routes can be defined at the bottom of the route map.
- _bindRoutes: function() {
- if (!this.routes) return;
- this.routes = _.result(this, 'routes');
- var route, routes = _.keys(this.routes);
- while ((route = routes.pop()) != null) {
- this.route(route, this.routes[route]);
- }
- },
- // Given a route, and a URL fragment that it matches, return the array of
- // extracted decoded parameters. Empty or unmatched parameters will be
- // treated as `null` to normalize cross-browser behavior.
- _extractParameters: function(route, fragment) {
- var params = route.exec(fragment).slice(1);
- return _.map(params, function(param, i) {
- // Don't decode the search params.
- if (i === params.length - 1) return param || null;
- return param ? decodeURIComponent(param) : null;
- });
- }
- });
- Backbone.Router.extend = function(protoProps, staticProps) {
- var parent = this;
- var child;
- // The constructor function for the new subclass is either defined by you
- // (the "constructor" property in your `extend` definition), or defaulted
- // by us to simply call the parent's constructor.
- if (protoProps && _.has(protoProps, 'constructor')) {
- child = protoProps.constructor;
- } else {
- child = function(){ return parent.apply(this, arguments); };
- }
- // Add static properties to the constructor function, if supplied.
- //将静态方法和 parent上的静态方法一起扩展到child上面去
- _.extend(child, parent, staticProps);
- // Set the prototype chain to inherit from `parent`, without calling
- // `parent`'s constructor function.
- //创建一个新的构造含糊Surrogate ;
- //this.constructor = child的意思是 Surrogate实例化后的对象 让对象的构造函数指向child
- // Surrogate的原型就是parent的原型
- // 然后实例化给child的原型,
- // 这里不是直接从new parent给child.prototype 而是创建一个新的构造函数,我也不知道为啥要这样
- var Surrogate = function(){ this.constructor = child; };
- Surrogate.prototype = parent.prototype;
- child.prototype = new Surrogate;
- // Add prototype properties (instance properties) to the subclass,
- // if supplied.
- // 把第一个参数上的属性扩展到child.prototype
- if (protoProps) _.extend(child.prototype, protoProps);
- // Set a convenience property in case the parent's prototype is needed
- // later.
- // 拿一个属性引用父的原型, 以免以后要用到.
- child.__super__ = parent.prototype;
- return child;
- }
- })();
- </script>
- </body>
- </html>
history怎么实现一个页面里面让浏览器前进后退了?用到了以下的方法
onhashchange pushstate
onhashchange 给window绑定onhashchange事件,当描点变化的时候,触发事件,然后就可以改变页面了
但onhashchange是从ie8开始能够支持,如果要做le6,7的兼容则必须用其他的办法,backbone的实现是创建一个隐藏的iframe,同时改变浏览器的url上的锚点和iframe的锚点,用一个定时器不停的监听浏览器url的变化
pushstate,html5里面新方法,给window绑定onpopstate方法,查看history.state的值,如果自己通过history.pushState改变了history.state的值,就当做该url之前已经存在,如果history.state是undefined,表示是新地址,要用history.pushState加到history里面去。
下面3个小demo分别是实现用onhashchange,pushstate和隐藏iframe实现浏览器前进和后退
onhashchange方式
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8"/>
- <title>Lottery Demo</title>
- <style type="text/css">
- *{padding:0;margin:0;}
- </style>
- </head>
- <body>
- <div id="wrap"></div>
- <script type="text/javascript" src="jquery.js"></script>
- <script type="text/javascript">
- var urlHash = {
- "one" : "我是第一页",
- "two" : "我是第二页",
- "three" : "我是第三页"
- }
- function c(){
- var hash = location.hash.replace("#","");
- if(hash in urlHash){
- $("#wrap").html(urlHash[hash]);
- }else{
- $("#wrap").html("该锚点没有对应的页面");
- }
- }
- function n(fragment){
- location.hash = fragment;
- }
- window.attachEvent
- ? window.attachEvent('onhashchange', c)
- : window.addEventListener("hashchange",c,false);
- n("one");
- //用n函数跳转描点
- </script>
- </body>
- </html>
pushstate
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8"/>
- <title>Lottery Demo</title>
- <style type="text/css">
- *{padding:0;margin:0;}
- </style>
- </head>
- <body>
- <div id="wrap"></div>
- <script type="text/javascript" src="jquery.js"></script>
- <script type="text/javascript">
- var urlHash = {
- "one" : "我是第一页",
- "two" : "我是第二页",
- "three" : "我是第三页"
- }
- function n(fragment){
- //history.pushState({lk:fragment,text:fragment}, "", fragment);
- var text =urlHash[fragment] || "没有对应的页面";
- history.pushState({lk:fragment,text:text}, "", fragment);
- $("#wrap").html(history.state.text);
- }
- window.addEventListener("popstate",function(){
- if(history.state){
- $("#wrap").html(history.state.text);
- }
- },false);
- n("one");
- //用n函数跳转描点
- </script>
- </body>
- </html>
用iframe的方式
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8"/>
- <title>Lottery Demo</title>
- <style type="text/css">
- *{padding:0;margin:0;}
- </style>
- </head>
- <body>
- <div id="wrap"></div>
- <script type="text/javascript" src="jquery.js"></script>
- <script type="text/javascript">
- var urlHash = {
- "one" : "我是第一页",
- "two" : "我是第二页",
- "three" : "我是第三页"
- }
- var iframe = document.createElement('iframe')
- iframe.src = 'javascript:0';
- iframe.style.display = 'none';
- iframe.tabIndex = -1;
- var iframeWindow = document.body.insertBefore(iframe, document.body.firstChild).contentWindow;
- var currFragment = null;
- function n(fragment){
- if(currFragment == fragment || fragment=="")return;
- currFragment = fragment;
- window.location.hash = '#' + fragment;
- window.iframeWindow.document.open().close();
- window.iframeWindow.location.hash = '#' + fragment;
- var text =urlHash[fragment] || "没有对应的页面";
- $("#wrap").html(text);
- }
- setInterval(function(){
- var fragment = window.location.hash.replace("#","")
- n(fragment)
- }, 50);
- //用n函数跳转描点
- </script>
- </body>
- </html>
Backbone.History
Backbone.History是一个类,Backbone.history才是Backbone.History实例化后的对象,即Backbone.history = new Backbone.History;
Backbone.History有以下比较重要的方法
start
要启动Backbone的路由功能,首先必须调用Backbone.history.start(),start方法做了以下一些事情
首页设置 History.started=true 表示该路由功能已经启动
然后看当前浏览器是否支持onhashChange,参数入的参数是希望通过hash的方式还是希望通过pushstate的方式改变url
根据传入的root来设置url路径将怎么改变,默认以当前的路径
如果浏览器不支持onhashchange(不支持hashchange肯定也不会支持pushstate的,因为onhashchange出现的时候,还没有出现pushstate),创建一个iframe
如果是pushstate方式 绑定window的popstate事件去监听url改变
如果是hashchange方式 绑定window的hashchange事件去监听url改变
如果不支持onhashchange,则设置一个定时器为监听url的改变
- var r = Backbone.history;
- r.navigate("one",true);//这里锚点是不会改变的 因为没有调用start方法
- r.start();
- r.navigate("one",true);//锚点可以改变了
getFragment
获取当前的锚点或者是地址(地址,暂且这么说吧,因为pushstate方式就是后面那串地址不同)
route (reg,callback)
添加对路由的监听事件,第一个参数是个正则,第2个参数是回调函数。以{route: route, callback: callback}的形式存在this.handlers中,当路由改变的时候,会遍历this.handlers,如果符合其中的route正则,则会执行相关回调
- var r = Backbone.history;
- r.route(/^a/,function(){alert("a")}); //监听描点如果是以a开头,则弹出a
- r.route(/^[^a]/,function(){alert("not a")}) //监听描点如果不是以a开头,则弹出not a
- r.start()
- r.navigate("aasd/asdf/ss",true)
- r.navigate("bxx/asdf/ss",true)
navigate (fragment, [options])
第一个参数个地址,需要进入的地址,第2个参数可以是对象,也可以是布尔值,true,表示路由要改变,如果有对应的回调事件也要执行,false表示不需要执行对应的回调事件,第2个参数当是对象的时候,trigger值得意义和布尔值的时候一样,replace值表示修改路由,但是不触发相应的回调事件,replace是不会记录在history里面,返回的时候不会有替换之前的那个地址.在然后改变url地址,设置了pushstate的用pushstate方式,支持onhashchang的直接用location.href来改变描点,不支持onhashchange的,除了改变location.href还要改变iframe的location.href。最后在触发相关监听路由的回调事件
在调用navigate方法的时候就会调用一次回调事件了,监听的时候也会调用一次回调事件的,所以在监听的时候会判断是不是同一个路由,同一个就不执行回调了,监听执行额回调事件也是在浏览器的前进和后退的时候在会执行,只用调用navigate,监听是不会执行回调的,是navigate方法里面主动调用的
一些例子
要启用路由功能一定要先调用start
- var r = Backbone.history;
- r.start();
- r.navigate("aaa",true)
- //r.navigate("aaa",{trigger:true}) 这种方式和上面那种方式是一样的
第二个参数如果设置成false,路由会改变,但是监听函数是不会触发的
- var r = Backbone.history;
- r.start();
- r.route(/^a/,function(){alert("a")})
- r.route(/^b/,function(){alert("b")})
- r.navigate("aaaa",true) //可以触发监听回调
- r.navigate("aaaa",false) //触发不了监听回调
如果第二个参数是一个对象,且设置了replace为true,改变路由后,之前的路由是不会记录在history里面的,而且监听路由的回调也不会执行
- var r = Backbone.history;
- r.start();
- r.route(/a^/,function(){alert(1)})
- r.navigate("as",{trigger:true,replace:true}) //监听路由的回调是不会执行的,且history中不会记录之前的路由
所以第二个参数false和replace的却别就在于histroy中是否记录之前的路由,他们的相同点是都不会触发监听回调
Backbone.Router
Backbone.Router基本上对Backbone.history一些方法的封装了.
route router.route(route, name, [callback])
route方法就是添加路由,路由对应的回调,他有3个参数,第一个参数就是路由了,第2个参数是方法名,如果传递了,当匹配域名的时候就会从Router这个对象里面去掉用该方法名,第3个参数也是回调,不设置第2个参数的时候就会调用第3个参数的回调
route的执行过程如下,首先判断route参数是不是正则表达式,如果不是则转成正则表达式,转换过程下
首先,把字符串route里面需要转义的字符全部转义,转义规则如下
- var escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g;
- route = route.replace(escapeRegExp, '\\$&');
需要转义的字符有\,-,{,},[,],+,.,\,?
然后把捕获变成非捕获,并且变成惰性匹配,因为这个正则后面要用到的是test方法,根本就不需要捕获,捕获会耗去更多的内存.
- var optionalParam = /\((.*?)\)/g;
- route = route.replace(optionalParam, '(?:$1)?')
然后在把 :\w+格式 转化成([^/?]+),路由上面的传参数的格式/:xxx,这个主要是把参数转换成([^/?]+),这个这则的意思就是不能是/和?的任意字符,因为参数的规则就是route/:arguments,如果带了/可能就路径错了,如果有?那就可能是url的参数
- var namedParam = /(\(\?)?:\w+/g;
- route = route.replace(namedParam, function(match, optional) {
- return optional ? match : '([^/?]+)';
- })
在然后 把点*以及*后面的字符串转化成([^?]*?),惰性匹配非?字符串 这个替换是干嘛用的还没看懂
- var splatParam = /\*\w+/g;
- route = route.replace(splatParam, '([^?]*?)');
最后给route字符串加上开头的符号和结尾的符号'^' + route + '(?:\\?([\\s\\S]*))?$',意思是说开头必须也route开头,后面可以跟参数,然后实例化这个字符串为正则对象并且返回
处理完正则之后,通过Backbone.history.route方法来监听该路由正则,如果在前进后退的时候路由匹配该正则就是执行回调
navigate (fragment, [options])
这个直接调用的Backbone.history.navigate(fragment, options);
- var r = new Backbone.Router;
- r.route("aa/:a/:a",function(){
- alert(Array.prototype.join.apply(arguments))
- })
- Backbone.history.start()
- r.navigate("aa/bb/cc",true)
- //传入bb cc 两个参数,可以打印出来
routes _bindRoutes
如果设置了routes,可以用_bindRoutes一次性绑定对routes里面的路由监听,_bindRoutes里面就是遍历routes,调用this,route进行绑定
- Backbone.Router = Backbone.Router.extend({
- alertA : function(){alert("a")}
- })
- var r = new Backbone.Router({
- routes : {
- "aa" : "alertA",
- "bb" : function(){alert("b")},
- }
- })
- Backbone.history.start()
- //先扩展alertA方法 然后通过routes批量绑定,如果value对应的是字符串,则会在实例化的r上找该方法,如果是函数就执行该函数
- r.navigate("aa",true);
- r.navigate("bb",true)
backbone.Router History源码笔记的更多相关文章
- Golang构建HTTP服务(一)--- net/http库源码笔记
搭建一个简单的Go Web服务器 Go语言标准库 - net/http 在学习Go语言有一个很好的起点,Go语言官方文档很详细,今天我们学习的Go Web服务器的搭建就需要用到Go语言官方提供的标准库 ...
- Backbone事件模块源码分析
事件模块Backbone.Events在Backbone中占有十分重要的位置,其他模块Model,Collection,View所有事件模块都依赖它.通过继承Events的方法来实现事件的管理,可以说 ...
- Zepto源码笔记(一)
最近在研究Zepto的源码,这是第一篇分析,欢迎大家继续关注,第一次写源码笔记,希望大家多指点指点,第一篇文章由于首次分析原因不会有太多干货,希望后面的文章能成为各位大大心目中的干货. Zepto是一 ...
- redis源码笔记(一) —— 从redis的启动到command的分发
本作品采用知识共享署名 4.0 国际许可协议进行许可.转载联系作者并保留声明头部与原文链接https://luzeshu.com/blog/redis1 本博客同步在http://www.cnblog ...
- AsyncTask源码笔记
AsyncTask源码笔记 AsyncTask在注释中建议只用来做短时间的异步操作,也就是只有几秒的操作:如果是长时间的操作,建议还是使用java.util.concurrent包中的工具类,例如Ex ...
- Java Arrays 源码 笔记
Arrays.java是Java中用来操作数组的类.使用这个工具类可以减少平常很多的工作量.了解其实现,可以避免一些错误的用法. 它提供的操作包括: 排序 sort 查找 binarySearch() ...
- Tomcat8源码笔记(八)明白Tomcat怎么部署webapps下项目
以前没想过这么个问题:Tomcat怎么处理webapps下项目,并且我访问浏览器ip: port/项目名/请求路径,以SSM为例,Tomcat怎么就能将请求找到项目呢,项目还是个文件夹类型的? Tom ...
- Tomcat8源码笔记(七)组件启动Server Service Engine Host启动
一.Tomcat启动的入口 Tomcat初始化简单流程前面博客介绍了一遍,组件除了StandardHost都有博客,欢迎大家指文中错误.Tomcat启动类是Bootstrap,而启动容器启动入口位于 ...
- Tomcat8源码笔记(六)连接器Connector分析
根据 Tomcat8源码笔记(五)组件Container分析 前文分析,StandardService的初始化重心由 StandardEngine转移到了Connector的初始化,本篇记录下Conn ...
随机推荐
- Java中static的用法
static静态,作为修饰符,最初是由c引入,一开始static表示退出一个块后依然存在的局部变量.随后,static表示不能被其他文件访问的全局变量和函数.到了C++和java,static表示属于 ...
- PostgreSQL停止动作观察
实验过程如下: 启动一个客户端: [postgres@cnrd56 bin]$ ./psql psql () Type "help" for help. postgres=# be ...
- 编程异常——假设你报createSQLQuery is not valid without active transaction,...
非常多时候我们使用hibernate的session时,都是让session在某一执行环境中保持其唯一. 比如在同一线程内用同一个session.在同一方法内用同一session,这样我们就能够用se ...
- linux C 执行多个文件
- android复制数据库到SD卡(网上搜集,未经验证)
android中使用sqlite.复制assets下的数据库到SD卡.支持大于1M的文件 如果使用SD卡,需要在AndroidManifest.xml中设置权限 <uses-permission ...
- JavaScript OOP 思想
JS的核心是对象 {}, new function(){}这种形式也是对象. http://www.nowamagic.net/librarys/veda/detail/241 整理一些网上的资料,供 ...
- input text框和 checkbox 连带被选中的情况
<html> <head></head> <body> <ul> <li><input type="checkb ...
- 网络编程之PC版与Android手机版带断点续传的多线程下载
一.多线程下载 多线程下载就是抢占服务器资源 原理:服务器CPU 分配给每条线程的时间片相同,服务器带宽平均分配给每条线程,所以客户端开启的线程越多,就能抢占到更多的服 ...
- JS的setTimeout函数第一个参数问题
setTimeout的第一个参数只能放一个无参的函数,更像放了一个函数指针在那里,如果要放带参数的话,就要拿个匿名函数包裹一下
- listen和accept函数
listen函数是用来设置监听连接的句柄和队列 当listen函数执行完成以后,服务端就已经可以接受客户端来的新连接了,新连接完成以后listen会把客户端的ip,port和连接句柄放在监听队列里面, ...