Skip to content

Commit

Permalink
Merge branch 'master' into landscaper/4217-use-strict
Browse files Browse the repository at this point in the history
  • Loading branch information
chasenlehara committed Jul 4, 2018
2 parents a336f25 + bbba59a commit 11e9413
Show file tree
Hide file tree
Showing 23 changed files with 245 additions and 305 deletions.
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
end_of_line = LF
indent_style = tab
trim_trailing_whitespace = false
insert_final_newline = true
insert_final_newline = true
indent_size = 2
2 changes: 1 addition & 1 deletion can-route.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var DefineMap = require("can-define/map/map");


route("{page}/{id}");
route.register("{page}/{id}");
route.ready();


Expand Down
94 changes: 65 additions & 29 deletions can-route.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";
/*jshint -W079 */
var Bind = require("can-bind");
var queues = require("can-queues");
var Observation = require('can-observation');

Expand All @@ -15,15 +16,16 @@ var urlHelpers = require("./src/url-helpers");
var routeParam = require("./src/param");
var routeDeparam = require("./src/deparam");
var bindingProxy = require("./src/binding-proxy");
var hashchange = require("./src/hashchange");
var Hashchange = require("can-route-hash");

var isWebWorker = require('can-globals/is-web-worker/is-web-worker');
var isBrowserWindow = require('can-globals/is-browser-window/is-browser-window');



bindingProxy.bindings.hashchange = hashchange;
var hashchangeObservable = new Hashchange();
bindingProxy.bindings.hashchange = hashchangeObservable;
bindingProxy.defaultBinding = "hashchange";
bindingProxy.urlDataObservable.value = hashchangeObservable;


// ## route.js
// `can-route`
Expand All @@ -32,7 +34,9 @@ bindingProxy.defaultBinding = "hashchange";

function canRoute(url, defaults){
//!steal-remove-start
devLog.warn('Call route.register(url,defaults) instead of calling route(url, defaults)');
if(process.env.NODE_ENV !== 'production') {
devLog.warn('Call route.register(url,defaults) instead of calling route(url, defaults)');
}
//!steal-remove-end
registerRoute.register(url, defaults);
return canRoute;
Expand Down Expand Up @@ -70,13 +74,6 @@ function updateUrl(serializedData) {
}, 10);
}

//!steal-remove-start
Object.defineProperty(updateUrl, "name", {
value: "can-route.updateUrl"
});
//!steal-remove-end


// Deparameterizes the portion of the hash of interest and assign the
// values to the `route.data` removing existing values no longer in the hash.
// updateRouteData is called typically by hashchange which fires asynchronously
Expand All @@ -96,11 +93,6 @@ function updateRouteData() {
queues.batch.stop();

}
//!steal-remove-start
Object.defineProperty(updateRouteData, "name", {
value: "can-route.updateRouteData"
});
//!steal-remove-end


/**
Expand Down Expand Up @@ -139,14 +131,19 @@ Object.defineProperty(canRoute,"defaultBinding",{
},
set: function(newVal){
bindingProxy.defaultBinding = newVal;
var observable = bindingProxy.bindings[bindingProxy.defaultBinding];
if(observable) {
bindingProxy.urlDataObservable.value = observable;
}
}
});
Object.defineProperty(canRoute,"currentBinding",{
Object.defineProperty(canRoute,"urlData",{
get: function(){
return bindingProxy.currentBinding;
return bindingProxy.urlDataObservable.value;
},
set: function(newVal){
bindingProxy.currentBinding = newVal;
canRoute._teardown();
bindingProxy.urlDataObservable.value = newVal;
}
});

Expand All @@ -155,7 +152,9 @@ canReflect.assignMap(canRoute, {
deparam: routeDeparam,
map: function(data){
//!steal-remove-start
devLog.warn('Set route.data directly instead of calling route.map');
if(process.env.NODE_ENV !== 'production') {
devLog.warn('Set route.data directly instead of calling route.map');
}
//!steal-remove-end
canRoute.data = data;
},
Expand Down Expand Up @@ -197,17 +196,54 @@ canReflect.assignMap(canRoute, {

// called when the route is ready
_setup: function () {
if (!canRoute.currentBinding) {
bindingProxy.call("can.onValue", updateRouteData);
canReflect.onValue( canRoute.serializedObservation, updateUrl, "notify");
canRoute.currentBinding =canRoute.defaultBinding;
if (!canRoute._canBinding) {

var bindingOptions = {

// The parent is the hashchange observable
parent: bindingProxy.urlDataObservable.value,
setParent: updateUrl,

// The child is route.data
child: canRoute.serializedObservation,
setChild: updateRouteData,

// On init, we do not want the child set to the parent’s value; this is
// handled by start() for reasons mentioned there.
onInitDoNotUpdateChild: true,

// Cycles are allowed because updateUrl is async; if another change
// happens during its setTimeout, then without cycles the change would
// be ignored :( TODO: Can this be removed if updateUrl stops using
// setTimeout in a major version?
cycles: 1,

// Listen for changes in the notify queue
queue: "notify"

};

// For debugging: the names that will be assigned to the updateChild and
// updateParent functions within can-bind
//!steal-remove-start
if (process.env.NODE_ENV !== 'production') {
bindingOptions.updateChildName = "can-route.updateRouteData";
bindingOptions.updateParentName = "can-route.updateUrl";
}
//!steal-remove-end

// Create a new binding with can-bind
canRoute._canBinding = new Bind(bindingOptions);

// …and turn it on!
canRoute._canBinding.start();

}
},
_teardown: function () {
if (canRoute.currentBinding) {
bindingProxy.call("can.offValue", updateRouteData);
canReflect.offValue( canRoute.serializedObservation, updateUrl, "notify");
canRoute.currentBinding = null;
if (canRoute._canBinding) {
canRoute._canBinding.stop();
canRoute._canBinding = null;
}
clearTimeout(timer);
},
Expand Down
2 changes: 1 addition & 1 deletion demos/counter.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
});

route("",{count: 0});
route.register("",{count: 0});

window.myCounter = new Counter();
route.data = myCounter;
Expand Down
10 changes: 6 additions & 4 deletions doc/can-route.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@

```js
{
data, // The bound observable.
data, // The bound key-value observable.
urlData, // The observable that represents the
// hash. Defaults to RouteHash.
register, // Register routes that translate between
// the url and the bound observable.
// the url and the bound observable.
start, // Begin updating the bound observable with
// url data and vice versa.
// url data and vice versa.
deparam, // Given url fragment, return the data for it.
rule, // Given url fragment, return the routing rule
param, // Given data, return a url fragment.
url, // Given data, return a url for it.
link, // Given data, return an <a> tag for it.
isCurrent, // Given data, return true if the current url matches
// the data.
// the data.
currentRule // Return the matched rule name.
}
```
Expand Down
4 changes: 2 additions & 2 deletions doc/currentRule.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
Use `route.currentRule()` to find the current route rule.

```js
route( "{type}", { type: "foo" } );
route( "{type}/{subtype}" );
route.register( "{type}", { type: "foo" } );
route.register( "{type}/{subtype}" );
route.currentRule(); // "{type}"
route.data.subtype = "foo";
route.currentRule(); // "{type}/{subtype}"
Expand Down
2 changes: 1 addition & 1 deletion doc/data.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@property {Object|HTMLElement} can-route.data data
@parent can-route.static

This is the internal observable object underlying [can-route]. It can be set in order to cross-bind a top level state object (Application ViewModel) to can-route.
An observable key-value object used to cross bind to the url observable [can-route.urlData]. Set it to cross-bind a top level state object (Application ViewModel) to [can-route].

@type {Object} If `route.data` is set to a [can-reflect]ed observable object of
key-value pairs, once [can-route.start] is called, changes in `route.data`'s
Expand Down
51 changes: 51 additions & 0 deletions doc/urlData.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
@property {ValueObservable} can-route.urlData urlData
@parent can-route.static

Specifies an observable value that represents the URL. Useful for changing
what URL [can-route route] is cross-bound to.

@type {ValueObservable} `urlData` is an observable value that represents the part of the URL cross
bound to the [can-route.data] state object. It can be set to other observable urls like [can-route-pushstate]
or [can-route-mock]. It defaults to [can-route-hash].

The following shows setting `urlData` to another observable.

```js
import {route, RouteMock, DefineMap} from "can";

// route.data will update routeMock and be updated by changes in
// routeMock.
var routeMock = route.urlData = new RouteMock();
var routeData = route.data = new DefineMap({},false);

// begin binding
route.start()

// simulate setting the URL
routeMock.value = "foo=bar";

routeData.foo //-> "bar";
```
@codepen

@body

## Creating your own `ValueObservable`

> WARNING: The following is non-normative and may change in a
> future release. Please let us know if you are trying to create your own
> observable and we will work with you to stabilize the API.

Besides implementing the standard `ValueObservable` symbols:

- [can-reflect.getValue]
- [can-reflect.setValue]
- [can-reflect/observe.onValue]
- [can-reflect/observe.offValue]

The `ValueObservable` should include the following properties:

- `paramsMatcher` - A regular expression used to test if the URL is formatted correctly for [can-route.deparam].
- `querySeparator` - A string that separates when arbitrary key-value pairs begin in the url (Example: `"?"`).
- `root` - A string value used to identify the part of the url where routing begins. For example, [can-route-hash] defaults to `"#!"`
16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "can-route",
"version": "4.1.2",
"version": "4.2.0",
"description": "Observable front-end application routing for CanJS.",
"homepage": "https://canjs.com/doc/can-route.html",
"license": "MIT",
Expand Down Expand Up @@ -33,7 +33,9 @@
"donejs"
],
"dependencies": {
"can-bind": "<2.0.0",
"can-deparam": "^1.0.1",
"can-diff": "^1.0.4",
"can-dom-events": "^1.1.0",
"can-event-queue": "<2.0.0",
"can-globals": "<2.0.0",
Expand All @@ -44,23 +46,25 @@
"can-observation-recorder": "<2.0.0",
"can-param": "^1.0.1",
"can-queues": "<2.0.0",
"can-reflect": "^1.6.0",
"can-reflect": "^1.16.7",
"can-route-hash": "<2.0.0",
"can-simple-map": "^4.0.0",
"can-simple-observable": "^2.0.0",
"can-symbol": "^1.0.0",
"can-util": "^3.9.0"
"can-string": "<2.0.0",
"can-symbol": "^1.0.0"
},
"devDependencies": {
"can-define": "^2.0.0",
"can-map": "^4.0.0",
"can-observe": "^2.0.0",
"can-route-mock": "<2.0.0",
"can-stache-key": "^1.0.0",
"detect-cyclic-packages": "^1.1.0",
"done-serve": "^1.5.0",
"done-serve": "^2.0.0",
"jshint": "^2.9.1",
"steal": "^1.2.9",
"steal-qunit": "^1.0.1",
"steal-tools": "^1.1.2",
"testee": "^0.7.0"
"testee": "^0.8.0"
}
}
18 changes: 8 additions & 10 deletions src/binding-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@ var canReflect = require('can-reflect');
var canSymbol = require("can-symbol");
var SimpleObservable = require("can-simple-observable");

var defaultBinding = new SimpleObservable("hashchange");
var urlDataObservable = new SimpleObservable(null);

var bindingProxy = {
get defaultBinding(){
return defaultBinding.get();
},
set defaultBinding(newVal){
defaultBinding.set(newVal);
},
currentBinding: null,
defaultBinding: null,
urlDataObservable: urlDataObservable,
bindings: {},
call: function(){
var args = canReflect.toArray(arguments),
prop = args.shift(),
binding = bindingProxy.bindings[bindingProxy.currentBinding ||bindingProxy.defaultBinding],
method = binding[prop.indexOf("can.") === 0 ? canSymbol.for(prop) : prop];
binding = urlDataObservable.value;
if(binding === null) {
throw new Error("there is no current binding!!!");
}
var method = binding[prop.indexOf("can.") === 0 ? canSymbol.for(prop) : prop];
if (method.apply) {
return method.apply(binding, args);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/deparam.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function canRoute_getRule(url){
* Extract data from a url, creating an object representing its values.
*
* ```js
* route("{page}");
* route.register("{page}");
*
* var result = route.deparam("page=home");
* console.log(result.page); // -> "home"
Expand Down Expand Up @@ -89,7 +89,7 @@ function canRoute_getRule(url){
* data object.
*
* ```js
* route("{type}/{id}");
* route.register("{type}/{id}");
*
* route.deparam("videos/5");
* // -> { id: 5, route: "{type}/{id}", type: "videos" }
Expand Down
Loading

0 comments on commit 11e9413

Please sign in to comment.