can-route
Advanced tools
Comparing version 3.0.0-pre.1 to 3.0.0-pre.3
120
can-route.js
/*jshint -W079 */ | ||
var Map = require('can-map'); | ||
var canBatch = require('can-event/batch/batch'); | ||
var canEvent = require('can-event'); | ||
var ObserveInfo = require('can-observe-info'); | ||
var compute = require('can-compute'); | ||
@@ -20,3 +20,2 @@ var deparam = require('can-util/js/deparam/deparam'); | ||
require('can-list'); | ||
@@ -82,4 +81,4 @@ // ## route.js | ||
if (obj && typeof obj === "object") { | ||
if (obj instanceof Map) { | ||
obj = obj; | ||
if (obj && typeof obj === "object" && ("serialize" in obj)) { | ||
obj = obj.serialize(); | ||
} else { | ||
@@ -176,9 +175,14 @@ // Get array from array-like or shallow-copy object | ||
// This might be able to use batchNum and avoid this. | ||
var onRouteDataChange = function (ev, attr, how, newval) { | ||
var oldProperties = null; | ||
var onRouteDataChange = function (ev, newProps, oldProps) { | ||
// indicate that data is changing | ||
changingData = 1; | ||
// collect attributes that are changing | ||
changedAttrs.push(attr); | ||
if(!oldProperties) { | ||
oldProperties = oldProps; | ||
} | ||
clearTimeout(timer); | ||
timer = setTimeout(function () { | ||
var old = oldProperties; | ||
oldProperties = null; | ||
// indicate that the hash is set to look like the data | ||
@@ -188,3 +192,3 @@ changingData = 0; | ||
path =canRoute.param(serialized, true); | ||
canRoute._call("setURL", path, changedAttrs); | ||
canRoute._call("setURL", path, newProps, old); | ||
// trigger a url change so its possible to live-bind on url-based changes | ||
@@ -200,2 +204,3 @@ canBatch.trigger(eventsObject,"__url",[path, lastHash]); | ||
var stringCoercingMapDecorator = function(map) { | ||
var attrSuper = map.attr; | ||
@@ -222,3 +227,8 @@ | ||
if(cur[attr] === undefined){ | ||
data.removeAttr(attr); | ||
if("removeAttr" in data) { | ||
data.removeAttr(attr); | ||
} else { | ||
cur[attr] = undefined; | ||
} | ||
} | ||
@@ -435,19 +445,4 @@ else if(Object.prototype.toString.call(old[attr]) === "[object Object]") { | ||
}, | ||
/** | ||
* @hide | ||
* A Map that represents the state of the history. | ||
*/ | ||
data: stringCoercingMapDecorator(new Map({})), | ||
map: function(data){ | ||
var appState; | ||
// appState is a Map constructor function | ||
if(data.prototype instanceof Map){ | ||
appState = new data(); | ||
} | ||
// appState is an instance of Map | ||
else { | ||
appState = data; | ||
} | ||
canRoute.data = stringCoercingMapDecorator(appState); | ||
canRoute.data = data; | ||
}, | ||
@@ -667,3 +662,3 @@ /** | ||
canRoute._call("bind"); | ||
canRoute.bind("change", onRouteDataChange); | ||
canRoute.serializedCompute.addEventListener("change", onRouteDataChange); | ||
canRoute.currentBinding =canRoute.defaultBinding; | ||
@@ -675,3 +670,3 @@ } | ||
canRoute._call("unbind"); | ||
canRoute.unbind("change", onRouteDataChange); | ||
canRoute.serializedCompute.removeEventListener("change", onRouteDataChange); | ||
canRoute.currentBinding = null; | ||
@@ -710,6 +705,75 @@ } | ||
canRoute.attr = function () { | ||
return canRoute.data.attr.apply(canRoute.data, arguments); | ||
var routeData; | ||
var setRouteData = function(data){ | ||
routeData = data; | ||
return routeData; | ||
}; | ||
var serializedCompute; | ||
Object.defineProperty(canRoute,"serializedCompute", { | ||
get: function(){ | ||
if(!serializedCompute) { | ||
serializedCompute = compute(function(){ | ||
return canRoute.data.serialize(); | ||
}); | ||
} | ||
return serializedCompute; | ||
} | ||
}); | ||
Object.defineProperty(canRoute,"data", { | ||
get: function(){ | ||
if(routeData) { | ||
return routeData; | ||
} else if( types.DefaultMap ) { | ||
if( types.DefaultMap.prototype.toObject ) { | ||
var DefaultRouteMap = types.DefaultMap.extend({ | ||
seal: false | ||
},{ | ||
"*": { | ||
type: "string" | ||
} | ||
}); | ||
return setRouteData(new DefaultRouteMap()); | ||
} else { | ||
return setRouteData( stringCoercingMapDecorator( new types.DefaultMap() ) ); | ||
} | ||
} else { | ||
throw new Error("can.route.data accessed without being set"); | ||
} | ||
}, | ||
set: function(data) { | ||
if( types.isConstructor( data ) ){ | ||
data = new data(); | ||
} | ||
// if it's a map, we make it always set strings for backwards compat | ||
if( "attr" in data ) { | ||
setRouteData( stringCoercingMapDecorator(data) ); | ||
} else { | ||
setRouteData(data); | ||
} | ||
} | ||
}); | ||
canRoute.attr = function (prop, value) { | ||
if("attr" in canRoute.data) { | ||
return canRoute.data.attr.apply(canRoute.data, arguments); | ||
} else { | ||
if(arguments.length > 1) { | ||
canRoute.data.set(prop, value); | ||
return canRoute.data; | ||
} else if(typeof prop === 'object') { | ||
canRoute.data.set(prop); | ||
return canRoute.data; | ||
} else if(arguments.length === 1){ | ||
return canRoute.data.get(prop); | ||
} else { | ||
return canRoute.data.toObject(); | ||
} | ||
} | ||
}; | ||
//Allow for overriding of route batching by can.transaction | ||
@@ -716,0 +780,0 @@ canRoute.batch = canBatch; |
{ | ||
"name": "can-route", | ||
"version": "3.0.0-pre.1", | ||
"version": "3.0.0-pre.3", | ||
"description": "", | ||
@@ -57,9 +57,12 @@ "homepage": "", | ||
"dependencies": { | ||
"can-map": "^3.0.0-pre.1", | ||
"can-list": "^3.0.0-pre.1", | ||
"can-event": "^3.0.0-pre.2", | ||
"can-observe-info": "^3.0.0-pre.1", | ||
"can-util": "^3.0.0-pre.2" | ||
"can-util": "^3.0.0-pre.15", | ||
"can-simple-map": "^3.0.0-pre.1", | ||
"can-compute": "3.0.0-pre.2" | ||
}, | ||
"devDependencies": { | ||
"can-map": "^3.0.0-pre.3", | ||
"can-list": "^3.0.0-pre.2", | ||
"can-define": "^0.7.3", | ||
"documentjs": "^0.4.2", | ||
@@ -66,0 +69,0 @@ "jshint": "^2.9.1", |
@@ -10,3 +10,3 @@ /* jshint asi:true */ | ||
QUnit.module("can/route", { | ||
QUnit.module("can/route with can-map", { | ||
setup: function () { | ||
@@ -578,6 +578,6 @@ canRoute._teardown(); | ||
test("canRoute.map: route is initialized from URL first, then URL params are added from canRoute.data", function(){ | ||
setupRouteTest(function (iframe, iCanRoute, loc) { | ||
setupRouteTest(function (iframe, iCanRoute, loc, win) { | ||
iCanRoute(":type/:id"); | ||
var AppState = Map.extend(); | ||
var AppState = win.CanMap.extend(); | ||
var appState = new AppState({section: 'home'}); | ||
@@ -584,0 +584,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
71013
16
2313
1
13
+ Addedcan-compute@3.0.0-pre.2
+ Addedcan-simple-map@^3.0.0-pre.1
+ Addedcan-compute@3.0.0-pre.2(transitive)
+ Addedcan-simple-map@3.3.2(transitive)
- Removedcan-list@^3.0.0-pre.1
- Removedcan-map@^3.0.0-pre.1
- Removedcan-compute@3.3.10(transitive)
- Removedcan-list@3.2.2(transitive)
- Removedcan-map@3.6.1(transitive)
- Removedcan-reflect-promise@1.1.5(transitive)
- Removedcan-stache-key@0.1.4(transitive)
Updatedcan-util@^3.0.0-pre.15