-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Updated examples to use codepen and indentation. * Moved documentation from inline to seperate files. This includes the following documentation - deparam - isCurrent - link - param - url All documentation is avaliable in the doc folder. * removed newline split from start.md * Updated examples and linked to Codepen. - currentRule.md - deparam.md - isCurrent.md - link.md - param.md - url.md * updated deparam documentation and example. * Incremental Changes to Examples and Docs `doc/currentRule.md` needed major updates to the signature example so it would work. This includes starting the route and adding Timeouts. Other changes are minor refactoring to improve over all consistency. * Updated examples and documentation for can-route and data. Fixed spacing in link and url. * minor doc and example updates. This includes removing examples that have been deemed redundant. * Deprecated route.link - There were some issues creating a deprecated group in can-route.md. Added to #209. * Added example to show possible use for using route.stop. * Minor documentation updates and examples * added note about how route.rule is used. * removed incorrect doc from rule.md * Small documentation updates * using Safari 11 in SauceLabs
- Loading branch information
1 parent
8838352
commit 8554cf5
Showing
16 changed files
with
688 additions
and
557 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
@function can-route.deparam deparam | ||
@parent can-route.static | ||
|
||
@description Extract data from a route path. | ||
|
||
@signature `route.deparam(url)` | ||
|
||
Extract data from a url fragment, creating an object representing its values. The url fragment could be a [location.hash](https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/hash) or [location.search](https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/search). | ||
|
||
```js | ||
import {route} from "can"; | ||
|
||
const result = route.deparam("page=home"); | ||
console.log( result.page ); //-> "home" | ||
``` | ||
@codepen | ||
|
||
@param {String} url A route fragment to extract data from. | ||
@return {Object} An object containing the extracted data. | ||
|
||
@body | ||
|
||
## Use | ||
|
||
`route.deparam` creates a data object based on the query string passed into it. This is useful to create an object based on the `location.hash`. | ||
|
||
```js | ||
import {route} from "can"; | ||
|
||
const result = route.deparam("id=5&type=videos"); | ||
console.log( result ); //-> { id: 5, type: "videos" } | ||
``` | ||
@codepen | ||
|
||
It's important to make sure the hash or exclamation point is not passed to `route.deparam` otherwise it will be included as a property. | ||
|
||
```html | ||
<mock-url></mock-url> | ||
<script type="module"> | ||
import "//unpkg.com/mock-url@^5.0.0"; | ||
import {route} from "can"; | ||
route.data = {}; | ||
route.register("") | ||
route.data.id = 5; // location.hash -> #!id=5 | ||
route.data.type = "videos"; // location.hash -> #!&id=5&type=videos | ||
route.start(); | ||
// setting datatype is synchronous | ||
setTimeout(() => { | ||
const result = route.deparam(location.hash); | ||
console.log( result ); //-> { #!: "", id: "5", type: "videos" } | ||
}, 300); | ||
</script> | ||
``` | ||
@codepen | ||
|
||
`route.deparam` will try and find a matching route and, if it does, will deconstruct the URL and parse out the key/value parameters into the data object. | ||
|
||
```js | ||
import {route} from "can"; | ||
|
||
route.register("{type}/{id}"); | ||
|
||
const result = route.deparam("videos/5"); | ||
console.log( result ); //-> { id: 5, type: "videos" } | ||
``` | ||
@codepen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
@function can-route.isCurrent isCurrent | ||
@parent can-route.static | ||
|
||
@description Check if data represents the current route. | ||
|
||
@signature `route.isCurrent(data [,subsetMatch] )` | ||
|
||
Compares `data` to the current route. Used to verify if an object is | ||
representative of the current route. | ||
|
||
The following example calls `route.isCurrent` with a single matching parameter when `route.data` has two properties. If `subsetMatch` is `false` or left default `route.isCurrent` won't try and match subsets. | ||
|
||
```js | ||
import {route} from "can"; | ||
|
||
route.data = {page: "recipes", id: "5"}; // location.hash -> "#!&page=recipes&id=5" | ||
route.start(); | ||
|
||
setTimeout(() => { | ||
const completeSet = route.isCurrent( {page: "recipes"} ); | ||
console.log( completeSet ); //-> false | ||
|
||
const subSet = route.isCurrent( {page: "recipes"}, true ); | ||
console.log( subSet ); //-> true | ||
}, 200); | ||
``` | ||
@codepen | ||
|
||
@param {Object} data Data to check against the current route. | ||
@param {Boolean} [subsetMatch] If true, `route.current` will return true | ||
if every value in `data` matches the current route data, even if | ||
the route data has additional properties that are not matched. Defaults to `false` | ||
where every property needs to be present. | ||
@return {Boolean} Whether the data matches the current URL. |
Oops, something went wrong.