Edit File by line
/home/barbar84/www/wp-inclu.../js
File: backbone.js
// right here:
[1500] Fix | Delete
var collectionMethods = {forEach: 3, each: 3, map: 3, collect: 3, reduce: 0,
[1501] Fix | Delete
foldl: 0, inject: 0, reduceRight: 0, foldr: 0, find: 3, detect: 3, filter: 3,
[1502] Fix | Delete
select: 3, reject: 3, every: 3, all: 3, some: 3, any: 3, include: 3, includes: 3,
[1503] Fix | Delete
contains: 3, invoke: 0, max: 3, min: 3, toArray: 1, size: 1, first: 3,
[1504] Fix | Delete
head: 3, take: 3, initial: 3, rest: 3, tail: 3, drop: 3, last: 3,
[1505] Fix | Delete
without: 0, difference: 0, indexOf: 3, shuffle: 1, lastIndexOf: 3,
[1506] Fix | Delete
isEmpty: 1, chain: 1, sample: 3, partition: 3, groupBy: 3, countBy: 3,
[1507] Fix | Delete
sortBy: 3, indexBy: 3, findIndex: 3, findLastIndex: 3};
[1508] Fix | Delete
[1509] Fix | Delete
[1510] Fix | Delete
// Underscore methods that we want to implement on the Model, mapped to the
[1511] Fix | Delete
// number of arguments they take.
[1512] Fix | Delete
var modelMethods = {keys: 1, values: 1, pairs: 1, invert: 1, pick: 0,
[1513] Fix | Delete
omit: 0, chain: 1, isEmpty: 1};
[1514] Fix | Delete
[1515] Fix | Delete
// Mix in each Underscore method as a proxy to `Collection#models`.
[1516] Fix | Delete
[1517] Fix | Delete
_.each([
[1518] Fix | Delete
[Collection, collectionMethods, 'models'],
[1519] Fix | Delete
[Model, modelMethods, 'attributes']
[1520] Fix | Delete
], function(config) {
[1521] Fix | Delete
var Base = config[0],
[1522] Fix | Delete
methods = config[1],
[1523] Fix | Delete
attribute = config[2];
[1524] Fix | Delete
[1525] Fix | Delete
Base.mixin = function(obj) {
[1526] Fix | Delete
var mappings = _.reduce(_.functions(obj), function(memo, name) {
[1527] Fix | Delete
memo[name] = 0;
[1528] Fix | Delete
return memo;
[1529] Fix | Delete
}, {});
[1530] Fix | Delete
addUnderscoreMethods(Base, obj, mappings, attribute);
[1531] Fix | Delete
};
[1532] Fix | Delete
[1533] Fix | Delete
addUnderscoreMethods(Base, _, methods, attribute);
[1534] Fix | Delete
});
[1535] Fix | Delete
[1536] Fix | Delete
// Backbone.sync
[1537] Fix | Delete
// -------------
[1538] Fix | Delete
[1539] Fix | Delete
// Override this function to change the manner in which Backbone persists
[1540] Fix | Delete
// models to the server. You will be passed the type of request, and the
[1541] Fix | Delete
// model in question. By default, makes a RESTful Ajax request
[1542] Fix | Delete
// to the model's `url()`. Some possible customizations could be:
[1543] Fix | Delete
//
[1544] Fix | Delete
// * Use `setTimeout` to batch rapid-fire updates into a single request.
[1545] Fix | Delete
// * Send up the models as XML instead of JSON.
[1546] Fix | Delete
// * Persist models via WebSockets instead of Ajax.
[1547] Fix | Delete
//
[1548] Fix | Delete
// Turn on `Backbone.emulateHTTP` in order to send `PUT` and `DELETE` requests
[1549] Fix | Delete
// as `POST`, with a `_method` parameter containing the true HTTP method,
[1550] Fix | Delete
// as well as all requests with the body as `application/x-www-form-urlencoded`
[1551] Fix | Delete
// instead of `application/json` with the model in a param named `model`.
[1552] Fix | Delete
// Useful when interfacing with server-side languages like **PHP** that make
[1553] Fix | Delete
// it difficult to read the body of `PUT` requests.
[1554] Fix | Delete
Backbone.sync = function(method, model, options) {
[1555] Fix | Delete
var type = methodMap[method];
[1556] Fix | Delete
[1557] Fix | Delete
// Default options, unless specified.
[1558] Fix | Delete
_.defaults(options || (options = {}), {
[1559] Fix | Delete
emulateHTTP: Backbone.emulateHTTP,
[1560] Fix | Delete
emulateJSON: Backbone.emulateJSON
[1561] Fix | Delete
});
[1562] Fix | Delete
[1563] Fix | Delete
// Default JSON-request options.
[1564] Fix | Delete
var params = {type: type, dataType: 'json'};
[1565] Fix | Delete
[1566] Fix | Delete
// Ensure that we have a URL.
[1567] Fix | Delete
if (!options.url) {
[1568] Fix | Delete
params.url = _.result(model, 'url') || urlError();
[1569] Fix | Delete
}
[1570] Fix | Delete
[1571] Fix | Delete
// Ensure that we have the appropriate request data.
[1572] Fix | Delete
if (options.data == null && model && (method === 'create' || method === 'update' || method === 'patch')) {
[1573] Fix | Delete
params.contentType = 'application/json';
[1574] Fix | Delete
params.data = JSON.stringify(options.attrs || model.toJSON(options));
[1575] Fix | Delete
}
[1576] Fix | Delete
[1577] Fix | Delete
// For older servers, emulate JSON by encoding the request into an HTML-form.
[1578] Fix | Delete
if (options.emulateJSON) {
[1579] Fix | Delete
params.contentType = 'application/x-www-form-urlencoded';
[1580] Fix | Delete
params.data = params.data ? {model: params.data} : {};
[1581] Fix | Delete
}
[1582] Fix | Delete
[1583] Fix | Delete
// For older servers, emulate HTTP by mimicking the HTTP method with `_method`
[1584] Fix | Delete
// And an `X-HTTP-Method-Override` header.
[1585] Fix | Delete
if (options.emulateHTTP && (type === 'PUT' || type === 'DELETE' || type === 'PATCH')) {
[1586] Fix | Delete
params.type = 'POST';
[1587] Fix | Delete
if (options.emulateJSON) params.data._method = type;
[1588] Fix | Delete
var beforeSend = options.beforeSend;
[1589] Fix | Delete
options.beforeSend = function(xhr) {
[1590] Fix | Delete
xhr.setRequestHeader('X-HTTP-Method-Override', type);
[1591] Fix | Delete
if (beforeSend) return beforeSend.apply(this, arguments);
[1592] Fix | Delete
};
[1593] Fix | Delete
}
[1594] Fix | Delete
[1595] Fix | Delete
// Don't process data on a non-GET request.
[1596] Fix | Delete
if (params.type !== 'GET' && !options.emulateJSON) {
[1597] Fix | Delete
params.processData = false;
[1598] Fix | Delete
}
[1599] Fix | Delete
[1600] Fix | Delete
// Pass along `textStatus` and `errorThrown` from jQuery.
[1601] Fix | Delete
var error = options.error;
[1602] Fix | Delete
options.error = function(xhr, textStatus, errorThrown) {
[1603] Fix | Delete
options.textStatus = textStatus;
[1604] Fix | Delete
options.errorThrown = errorThrown;
[1605] Fix | Delete
if (error) error.call(options.context, xhr, textStatus, errorThrown);
[1606] Fix | Delete
};
[1607] Fix | Delete
[1608] Fix | Delete
// Make the request, allowing the user to override any Ajax options.
[1609] Fix | Delete
var xhr = options.xhr = Backbone.ajax(_.extend(params, options));
[1610] Fix | Delete
model.trigger('request', model, xhr, options);
[1611] Fix | Delete
return xhr;
[1612] Fix | Delete
};
[1613] Fix | Delete
[1614] Fix | Delete
// Map from CRUD to HTTP for our default `Backbone.sync` implementation.
[1615] Fix | Delete
var methodMap = {
[1616] Fix | Delete
create: 'POST',
[1617] Fix | Delete
update: 'PUT',
[1618] Fix | Delete
patch: 'PATCH',
[1619] Fix | Delete
delete: 'DELETE',
[1620] Fix | Delete
read: 'GET'
[1621] Fix | Delete
};
[1622] Fix | Delete
[1623] Fix | Delete
// Set the default implementation of `Backbone.ajax` to proxy through to `$`.
[1624] Fix | Delete
// Override this if you'd like to use a different library.
[1625] Fix | Delete
Backbone.ajax = function() {
[1626] Fix | Delete
return Backbone.$.ajax.apply(Backbone.$, arguments);
[1627] Fix | Delete
};
[1628] Fix | Delete
[1629] Fix | Delete
// Backbone.Router
[1630] Fix | Delete
// ---------------
[1631] Fix | Delete
[1632] Fix | Delete
// Routers map faux-URLs to actions, and fire events when routes are
[1633] Fix | Delete
// matched. Creating a new one sets its `routes` hash, if not set statically.
[1634] Fix | Delete
var Router = Backbone.Router = function(options) {
[1635] Fix | Delete
options || (options = {});
[1636] Fix | Delete
this.preinitialize.apply(this, arguments);
[1637] Fix | Delete
if (options.routes) this.routes = options.routes;
[1638] Fix | Delete
this._bindRoutes();
[1639] Fix | Delete
this.initialize.apply(this, arguments);
[1640] Fix | Delete
};
[1641] Fix | Delete
[1642] Fix | Delete
// Cached regular expressions for matching named param parts and splatted
[1643] Fix | Delete
// parts of route strings.
[1644] Fix | Delete
var optionalParam = /\((.*?)\)/g;
[1645] Fix | Delete
var namedParam = /(\(\?)?:\w+/g;
[1646] Fix | Delete
var splatParam = /\*\w+/g;
[1647] Fix | Delete
var escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g;
[1648] Fix | Delete
[1649] Fix | Delete
// Set up all inheritable **Backbone.Router** properties and methods.
[1650] Fix | Delete
_.extend(Router.prototype, Events, {
[1651] Fix | Delete
[1652] Fix | Delete
// preinitialize is an empty function by default. You can override it with a function
[1653] Fix | Delete
// or object. preinitialize will run before any instantiation logic is run in the Router.
[1654] Fix | Delete
preinitialize: function(){},
[1655] Fix | Delete
[1656] Fix | Delete
// Initialize is an empty function by default. Override it with your own
[1657] Fix | Delete
// initialization logic.
[1658] Fix | Delete
initialize: function(){},
[1659] Fix | Delete
[1660] Fix | Delete
// Manually bind a single named route to a callback. For example:
[1661] Fix | Delete
//
[1662] Fix | Delete
// this.route('search/:query/p:num', 'search', function(query, num) {
[1663] Fix | Delete
// ...
[1664] Fix | Delete
// });
[1665] Fix | Delete
//
[1666] Fix | Delete
route: function(route, name, callback) {
[1667] Fix | Delete
if (!_.isRegExp(route)) route = this._routeToRegExp(route);
[1668] Fix | Delete
if (_.isFunction(name)) {
[1669] Fix | Delete
callback = name;
[1670] Fix | Delete
name = '';
[1671] Fix | Delete
}
[1672] Fix | Delete
if (!callback) callback = this[name];
[1673] Fix | Delete
var router = this;
[1674] Fix | Delete
Backbone.history.route(route, function(fragment) {
[1675] Fix | Delete
var args = router._extractParameters(route, fragment);
[1676] Fix | Delete
if (router.execute(callback, args, name) !== false) {
[1677] Fix | Delete
router.trigger.apply(router, ['route:' + name].concat(args));
[1678] Fix | Delete
router.trigger('route', name, args);
[1679] Fix | Delete
Backbone.history.trigger('route', router, name, args);
[1680] Fix | Delete
}
[1681] Fix | Delete
});
[1682] Fix | Delete
return this;
[1683] Fix | Delete
},
[1684] Fix | Delete
[1685] Fix | Delete
// Execute a route handler with the provided parameters. This is an
[1686] Fix | Delete
// excellent place to do pre-route setup or post-route cleanup.
[1687] Fix | Delete
execute: function(callback, args, name) {
[1688] Fix | Delete
if (callback) callback.apply(this, args);
[1689] Fix | Delete
},
[1690] Fix | Delete
[1691] Fix | Delete
// Simple proxy to `Backbone.history` to save a fragment into the history.
[1692] Fix | Delete
navigate: function(fragment, options) {
[1693] Fix | Delete
Backbone.history.navigate(fragment, options);
[1694] Fix | Delete
return this;
[1695] Fix | Delete
},
[1696] Fix | Delete
[1697] Fix | Delete
// Bind all defined routes to `Backbone.history`. We have to reverse the
[1698] Fix | Delete
// order of the routes here to support behavior where the most general
[1699] Fix | Delete
// routes can be defined at the bottom of the route map.
[1700] Fix | Delete
_bindRoutes: function() {
[1701] Fix | Delete
if (!this.routes) return;
[1702] Fix | Delete
this.routes = _.result(this, 'routes');
[1703] Fix | Delete
var route, routes = _.keys(this.routes);
[1704] Fix | Delete
while ((route = routes.pop()) != null) {
[1705] Fix | Delete
this.route(route, this.routes[route]);
[1706] Fix | Delete
}
[1707] Fix | Delete
},
[1708] Fix | Delete
[1709] Fix | Delete
// Convert a route string into a regular expression, suitable for matching
[1710] Fix | Delete
// against the current location hash.
[1711] Fix | Delete
_routeToRegExp: function(route) {
[1712] Fix | Delete
route = route.replace(escapeRegExp, '\\$&')
[1713] Fix | Delete
.replace(optionalParam, '(?:$1)?')
[1714] Fix | Delete
.replace(namedParam, function(match, optional) {
[1715] Fix | Delete
return optional ? match : '([^/?]+)';
[1716] Fix | Delete
})
[1717] Fix | Delete
.replace(splatParam, '([^?]*?)');
[1718] Fix | Delete
return new RegExp('^' + route + '(?:\\?([\\s\\S]*))?$');
[1719] Fix | Delete
},
[1720] Fix | Delete
[1721] Fix | Delete
// Given a route, and a URL fragment that it matches, return the array of
[1722] Fix | Delete
// extracted decoded parameters. Empty or unmatched parameters will be
[1723] Fix | Delete
// treated as `null` to normalize cross-browser behavior.
[1724] Fix | Delete
_extractParameters: function(route, fragment) {
[1725] Fix | Delete
var params = route.exec(fragment).slice(1);
[1726] Fix | Delete
return _.map(params, function(param, i) {
[1727] Fix | Delete
// Don't decode the search params.
[1728] Fix | Delete
if (i === params.length - 1) return param || null;
[1729] Fix | Delete
return param ? decodeURIComponent(param) : null;
[1730] Fix | Delete
});
[1731] Fix | Delete
}
[1732] Fix | Delete
[1733] Fix | Delete
});
[1734] Fix | Delete
[1735] Fix | Delete
// Backbone.History
[1736] Fix | Delete
// ----------------
[1737] Fix | Delete
[1738] Fix | Delete
// Handles cross-browser history management, based on either
[1739] Fix | Delete
// [pushState](http://diveintohtml5.info/history.html) and real URLs, or
[1740] Fix | Delete
// [onhashchange](https://developer.mozilla.org/en-US/docs/DOM/window.onhashchange)
[1741] Fix | Delete
// and URL fragments. If the browser supports neither (old IE, natch),
[1742] Fix | Delete
// falls back to polling.
[1743] Fix | Delete
var History = Backbone.History = function() {
[1744] Fix | Delete
this.handlers = [];
[1745] Fix | Delete
this.checkUrl = this.checkUrl.bind(this);
[1746] Fix | Delete
[1747] Fix | Delete
// Ensure that `History` can be used outside of the browser.
[1748] Fix | Delete
if (typeof window !== 'undefined') {
[1749] Fix | Delete
this.location = window.location;
[1750] Fix | Delete
this.history = window.history;
[1751] Fix | Delete
}
[1752] Fix | Delete
};
[1753] Fix | Delete
[1754] Fix | Delete
// Cached regex for stripping a leading hash/slash and trailing space.
[1755] Fix | Delete
var routeStripper = /^[#\/]|\s+$/g;
[1756] Fix | Delete
[1757] Fix | Delete
// Cached regex for stripping leading and trailing slashes.
[1758] Fix | Delete
var rootStripper = /^\/+|\/+$/g;
[1759] Fix | Delete
[1760] Fix | Delete
// Cached regex for stripping urls of hash.
[1761] Fix | Delete
var pathStripper = /#.*$/;
[1762] Fix | Delete
[1763] Fix | Delete
// Has the history handling already been started?
[1764] Fix | Delete
History.started = false;
[1765] Fix | Delete
[1766] Fix | Delete
// Set up all inheritable **Backbone.History** properties and methods.
[1767] Fix | Delete
_.extend(History.prototype, Events, {
[1768] Fix | Delete
[1769] Fix | Delete
// The default interval to poll for hash changes, if necessary, is
[1770] Fix | Delete
// twenty times a second.
[1771] Fix | Delete
interval: 50,
[1772] Fix | Delete
[1773] Fix | Delete
// Are we at the app root?
[1774] Fix | Delete
atRoot: function() {
[1775] Fix | Delete
var path = this.location.pathname.replace(/[^\/]$/, '$&/');
[1776] Fix | Delete
return path === this.root && !this.getSearch();
[1777] Fix | Delete
},
[1778] Fix | Delete
[1779] Fix | Delete
// Does the pathname match the root?
[1780] Fix | Delete
matchRoot: function() {
[1781] Fix | Delete
var path = this.decodeFragment(this.location.pathname);
[1782] Fix | Delete
var rootPath = path.slice(0, this.root.length - 1) + '/';
[1783] Fix | Delete
return rootPath === this.root;
[1784] Fix | Delete
},
[1785] Fix | Delete
[1786] Fix | Delete
// Unicode characters in `location.pathname` are percent encoded so they're
[1787] Fix | Delete
// decoded for comparison. `%25` should not be decoded since it may be part
[1788] Fix | Delete
// of an encoded parameter.
[1789] Fix | Delete
decodeFragment: function(fragment) {
[1790] Fix | Delete
return decodeURI(fragment.replace(/%25/g, '%2525'));
[1791] Fix | Delete
},
[1792] Fix | Delete
[1793] Fix | Delete
// In IE6, the hash fragment and search params are incorrect if the
[1794] Fix | Delete
// fragment contains `?`.
[1795] Fix | Delete
getSearch: function() {
[1796] Fix | Delete
var match = this.location.href.replace(/#.*/, '').match(/\?.+/);
[1797] Fix | Delete
return match ? match[0] : '';
[1798] Fix | Delete
},
[1799] Fix | Delete
[1800] Fix | Delete
// Gets the true hash value. Cannot use location.hash directly due to bug
[1801] Fix | Delete
// in Firefox where location.hash will always be decoded.
[1802] Fix | Delete
getHash: function(window) {
[1803] Fix | Delete
var match = (window || this).location.href.match(/#(.*)$/);
[1804] Fix | Delete
return match ? match[1] : '';
[1805] Fix | Delete
},
[1806] Fix | Delete
[1807] Fix | Delete
// Get the pathname and search params, without the root.
[1808] Fix | Delete
getPath: function() {
[1809] Fix | Delete
var path = this.decodeFragment(
[1810] Fix | Delete
this.location.pathname + this.getSearch()
[1811] Fix | Delete
).slice(this.root.length - 1);
[1812] Fix | Delete
return path.charAt(0) === '/' ? path.slice(1) : path;
[1813] Fix | Delete
},
[1814] Fix | Delete
[1815] Fix | Delete
// Get the cross-browser normalized URL fragment from the path or hash.
[1816] Fix | Delete
getFragment: function(fragment) {
[1817] Fix | Delete
if (fragment == null) {
[1818] Fix | Delete
if (this._usePushState || !this._wantsHashChange) {
[1819] Fix | Delete
fragment = this.getPath();
[1820] Fix | Delete
} else {
[1821] Fix | Delete
fragment = this.getHash();
[1822] Fix | Delete
}
[1823] Fix | Delete
}
[1824] Fix | Delete
return fragment.replace(routeStripper, '');
[1825] Fix | Delete
},
[1826] Fix | Delete
[1827] Fix | Delete
// Start the hash change handling, returning `true` if the current URL matches
[1828] Fix | Delete
// an existing route, and `false` otherwise.
[1829] Fix | Delete
start: function(options) {
[1830] Fix | Delete
if (History.started) throw new Error('Backbone.history has already been started');
[1831] Fix | Delete
History.started = true;
[1832] Fix | Delete
[1833] Fix | Delete
// Figure out the initial configuration. Do we need an iframe?
[1834] Fix | Delete
// Is pushState desired ... is it available?
[1835] Fix | Delete
this.options = _.extend({root: '/'}, this.options, options);
[1836] Fix | Delete
this.root = this.options.root;
[1837] Fix | Delete
this._wantsHashChange = this.options.hashChange !== false;
[1838] Fix | Delete
this._hasHashChange = 'onhashchange' in window && (document.documentMode === void 0 || document.documentMode > 7);
[1839] Fix | Delete
this._useHashChange = this._wantsHashChange && this._hasHashChange;
[1840] Fix | Delete
this._wantsPushState = !!this.options.pushState;
[1841] Fix | Delete
this._hasPushState = !!(this.history && this.history.pushState);
[1842] Fix | Delete
this._usePushState = this._wantsPushState && this._hasPushState;
[1843] Fix | Delete
this.fragment = this.getFragment();
[1844] Fix | Delete
[1845] Fix | Delete
// Normalize root to always include a leading and trailing slash.
[1846] Fix | Delete
this.root = ('/' + this.root + '/').replace(rootStripper, '/');
[1847] Fix | Delete
[1848] Fix | Delete
// Transition from hashChange to pushState or vice versa if both are
[1849] Fix | Delete
// requested.
[1850] Fix | Delete
if (this._wantsHashChange && this._wantsPushState) {
[1851] Fix | Delete
[1852] Fix | Delete
// If we've started off with a route from a `pushState`-enabled
[1853] Fix | Delete
// browser, but we're currently in a browser that doesn't support it...
[1854] Fix | Delete
if (!this._hasPushState && !this.atRoot()) {
[1855] Fix | Delete
var rootPath = this.root.slice(0, -1) || '/';
[1856] Fix | Delete
this.location.replace(rootPath + '#' + this.getPath());
[1857] Fix | Delete
// Return immediately as browser will do redirect to new url
[1858] Fix | Delete
return true;
[1859] Fix | Delete
[1860] Fix | Delete
// Or if we've started out with a hash-based route, but we're currently
[1861] Fix | Delete
// in a browser where it could be `pushState`-based instead...
[1862] Fix | Delete
} else if (this._hasPushState && this.atRoot()) {
[1863] Fix | Delete
this.navigate(this.getHash(), {replace: true});
[1864] Fix | Delete
}
[1865] Fix | Delete
[1866] Fix | Delete
}
[1867] Fix | Delete
[1868] Fix | Delete
// Proxy an iframe to handle location events if the browser doesn't
[1869] Fix | Delete
// support the `hashchange` event, HTML5 history, or the user wants
[1870] Fix | Delete
// `hashChange` but not `pushState`.
[1871] Fix | Delete
if (!this._hasHashChange && this._wantsHashChange && !this._usePushState) {
[1872] Fix | Delete
this.iframe = document.createElement('iframe');
[1873] Fix | Delete
this.iframe.src = 'javascript:0';
[1874] Fix | Delete
this.iframe.style.display = 'none';
[1875] Fix | Delete
this.iframe.tabIndex = -1;
[1876] Fix | Delete
var body = document.body;
[1877] Fix | Delete
// Using `appendChild` will throw on IE < 9 if the document is not ready.
[1878] Fix | Delete
var iWindow = body.insertBefore(this.iframe, body.firstChild).contentWindow;
[1879] Fix | Delete
iWindow.document.open();
[1880] Fix | Delete
iWindow.document.close();
[1881] Fix | Delete
iWindow.location.hash = '#' + this.fragment;
[1882] Fix | Delete
}
[1883] Fix | Delete
[1884] Fix | Delete
// Add a cross-platform `addEventListener` shim for older browsers.
[1885] Fix | Delete
var addEventListener = window.addEventListener || function(eventName, listener) {
[1886] Fix | Delete
return attachEvent('on' + eventName, listener);
[1887] Fix | Delete
};
[1888] Fix | Delete
[1889] Fix | Delete
// Depending on whether we're using pushState or hashes, and whether
[1890] Fix | Delete
// 'onhashchange' is supported, determine how we check the URL state.
[1891] Fix | Delete
if (this._usePushState) {
[1892] Fix | Delete
addEventListener('popstate', this.checkUrl, false);
[1893] Fix | Delete
} else if (this._useHashChange && !this.iframe) {
[1894] Fix | Delete
addEventListener('hashchange', this.checkUrl, false);
[1895] Fix | Delete
} else if (this._wantsHashChange) {
[1896] Fix | Delete
this._checkUrlInterval = setInterval(this.checkUrl, this.interval);
[1897] Fix | Delete
}
[1898] Fix | Delete
[1899] Fix | Delete
if (!this.options.silent) return this.loadUrl();
[1900] Fix | Delete
},
[1901] Fix | Delete
[1902] Fix | Delete
// Disable Backbone.history, perhaps temporarily. Not useful in a real app,
[1903] Fix | Delete
// but possibly useful for unit testing Routers.
[1904] Fix | Delete
stop: function() {
[1905] Fix | Delete
// Add a cross-platform `removeEventListener` shim for older browsers.
[1906] Fix | Delete
var removeEventListener = window.removeEventListener || function(eventName, listener) {
[1907] Fix | Delete
return detachEvent('on' + eventName, listener);
[1908] Fix | Delete
};
[1909] Fix | Delete
[1910] Fix | Delete
// Remove window listeners.
[1911] Fix | Delete
if (this._usePushState) {
[1912] Fix | Delete
removeEventListener('popstate', this.checkUrl, false);
[1913] Fix | Delete
} else if (this._useHashChange && !this.iframe) {
[1914] Fix | Delete
removeEventListener('hashchange', this.checkUrl, false);
[1915] Fix | Delete
}
[1916] Fix | Delete
[1917] Fix | Delete
// Clean up the iframe if necessary.
[1918] Fix | Delete
if (this.iframe) {
[1919] Fix | Delete
document.body.removeChild(this.iframe);
[1920] Fix | Delete
this.iframe = null;
[1921] Fix | Delete
}
[1922] Fix | Delete
[1923] Fix | Delete
// Some environments will throw when clearing an undefined interval.
[1924] Fix | Delete
if (this._checkUrlInterval) clearInterval(this._checkUrlInterval);
[1925] Fix | Delete
History.started = false;
[1926] Fix | Delete
},
[1927] Fix | Delete
[1928] Fix | Delete
// Add a route to be tested when the fragment changes. Routes added later
[1929] Fix | Delete
// may override previous routes.
[1930] Fix | Delete
route: function(route, callback) {
[1931] Fix | Delete
this.handlers.unshift({route: route, callback: callback});
[1932] Fix | Delete
},
[1933] Fix | Delete
[1934] Fix | Delete
// Checks the current URL to see if it has changed, and if it has,
[1935] Fix | Delete
// calls `loadUrl`, normalizing across the hidden iframe.
[1936] Fix | Delete
checkUrl: function(e) {
[1937] Fix | Delete
var current = this.getFragment();
[1938] Fix | Delete
[1939] Fix | Delete
// If the user pressed the back button, the iframe's hash will have
[1940] Fix | Delete
// changed and we should use that for comparison.
[1941] Fix | Delete
if (current === this.fragment && this.iframe) {
[1942] Fix | Delete
current = this.getHash(this.iframe.contentWindow);
[1943] Fix | Delete
}
[1944] Fix | Delete
[1945] Fix | Delete
if (current === this.fragment) return false;
[1946] Fix | Delete
if (this.iframe) this.navigate(current);
[1947] Fix | Delete
this.loadUrl();
[1948] Fix | Delete
},
[1949] Fix | Delete
[1950] Fix | Delete
// Attempt to load the current URL fragment. If a route succeeds with a
[1951] Fix | Delete
// match, returns `true`. If no defined routes matches the fragment,
[1952] Fix | Delete
// returns `false`.
[1953] Fix | Delete
loadUrl: function(fragment) {
[1954] Fix | Delete
// If the root doesn't match, no routes can match either.
[1955] Fix | Delete
if (!this.matchRoot()) return false;
[1956] Fix | Delete
fragment = this.fragment = this.getFragment(fragment);
[1957] Fix | Delete
return _.some(this.handlers, function(handler) {
[1958] Fix | Delete
if (handler.route.test(fragment)) {
[1959] Fix | Delete
handler.callback(fragment);
[1960] Fix | Delete
return true;
[1961] Fix | Delete
}
[1962] Fix | Delete
});
[1963] Fix | Delete
},
[1964] Fix | Delete
[1965] Fix | Delete
// Save a fragment into the hash history, or replace the URL state if the
[1966] Fix | Delete
// 'replace' option is passed. You are responsible for properly URL-encoding
[1967] Fix | Delete
// the fragment in advance.
[1968] Fix | Delete
//
[1969] Fix | Delete
// The options object can contain `trigger: true` if you wish to have the
[1970] Fix | Delete
// route callback be fired (not usually desirable), or `replace: true`, if
[1971] Fix | Delete
// you wish to modify the current URL without adding an entry to the history.
[1972] Fix | Delete
navigate: function(fragment, options) {
[1973] Fix | Delete
if (!History.started) return false;
[1974] Fix | Delete
if (!options || options === true) options = {trigger: !!options};
[1975] Fix | Delete
[1976] Fix | Delete
// Normalize the fragment.
[1977] Fix | Delete
fragment = this.getFragment(fragment || '');
[1978] Fix | Delete
[1979] Fix | Delete
// Don't include a trailing slash on the root.
[1980] Fix | Delete
var rootPath = this.root;
[1981] Fix | Delete
if (fragment === '' || fragment.charAt(0) === '?') {
[1982] Fix | Delete
rootPath = rootPath.slice(0, -1) || '/';
[1983] Fix | Delete
}
[1984] Fix | Delete
var url = rootPath + fragment;
[1985] Fix | Delete
[1986] Fix | Delete
// Strip the fragment of the query and hash for matching.
[1987] Fix | Delete
fragment = fragment.replace(pathStripper, '');
[1988] Fix | Delete
[1989] Fix | Delete
// Decode for matching.
[1990] Fix | Delete
var decodedFragment = this.decodeFragment(fragment);
[1991] Fix | Delete
[1992] Fix | Delete
if (this.fragment === decodedFragment) return;
[1993] Fix | Delete
this.fragment = decodedFragment;
[1994] Fix | Delete
[1995] Fix | Delete
// If pushState is available, we use it to set the fragment as a real URL.
[1996] Fix | Delete
if (this._usePushState) {
[1997] Fix | Delete
this.history[options.replace ? 'replaceState' : 'pushState']({}, document.title, url);
[1998] Fix | Delete
[1999] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function