Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Define path and query param propagation semantics. #40

Merged
merged 1 commit into from
Mar 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
"iron-test-helpers": "polymerelements/iron-test-helpers#^1.0.0"
},
"dependencies": {
"polymer": "polymer/polymer#^1.2.3"
"polymer": "polymer/polymer#^1.3.1"
}
}
6 changes: 1 addition & 5 deletions carbon-location.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@
`https://elements.polymer-project.org/elements/carbon-route-converter?foo=bar&baz=qux`:
{
prefix: '',
path: '/elements/carbon-route-converter',
queryParams: {
foo: 'bar',
baz: 'qux'
}
path: '/elements/carbon-route-converter'
}

Example Usage:
Expand Down
8 changes: 4 additions & 4 deletions carbon-route-converter.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@
observers: [
'_locationChanged(path, queryParams)',
'_routeChanged(route.prefix, route.path)',
'_routeQueryParamsChanged(route.queryParams)'
'_routeQueryParamsChanged(route.__queryParams)'
],

created: function() {
this.linkPaths('route.queryParams', 'queryParams');
this.linkPaths('queryParams', 'route.queryParams');
this.linkPaths('route.__queryParams', 'queryParams');
this.linkPaths('queryParams', 'route.__queryParams');
},

/**
Expand All @@ -119,7 +119,7 @@
this.route = {
prefix: '',
path: path,
queryParams: queryParams
__queryParams: queryParams
};
},

Expand Down
67 changes: 49 additions & 18 deletions carbon-route.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,20 @@
notify: true
},

queryParams: {
type: Object,
value: function() {
return {};
},
notify: true
},

/**
* The part of `path` NOT consumed by `pattern`.
*/
tail: {
type: Object,
value: function() {return {path: null, prefix: null, queryParams: null};},
value: function() {return {path: null, prefix: null, __queryParams: null};},
notify: true
},

Expand All @@ -138,45 +146,68 @@
'__tryToMatch(route.path, pattern)',
'__updatePathOnDataChange(data.*)',
'__tailPathChanged(tail.path)',
'__routeQueryParamsChanged(route.queryParams)',
'__tailQueryParamsChanged(tail.queryParams)'
'__routeQueryParamsChanged(route.__queryParams)',
'__tailQueryParamsChanged(tail.__queryParams)',
'__queryParamsChanged(queryParams.*)'
],

created: function() {
this.linkPaths('route.queryParams', 'tail.queryParams');
this.linkPaths('tail.queryParams', 'route.queryParams');
this.linkPaths('route.__queryParams', 'tail.__queryParams');
this.linkPaths('tail.__queryParams', 'route.__queryParams');
},

// Deal with the query params object being assigned to wholesale
__routeQueryParamsChanged: function(queryParams) {
if (queryParams && this.tail) {
this.set('tail.queryParams', queryParams);
this.set('tail.__queryParams', queryParams);

if (!this.active || this._skipMatch) {
return;
}

this._skipMatch = true;
this.set('queryParams', Object.assign({}, queryParams));
this._skipMatch = false;
}
},

__tailQueryParamsChanged: function(queryParams) {
if (queryParams && this.route) {
this.set('route.queryParams', queryParams);
this.set('route.__queryParams', queryParams);
}
},

__queryParamsChanged: function(changes) {
if (!this.active || this._skipMatch) {
return;
}

this.set('route.__' + changes.path, changes.value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will changes.path and changes.value exist even when the changes are splices? I guess we can probably ignore that case though, because we don't support arrays inside of queryParams currently

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't, but a derived element might. But, maybe we should table implementation until we have true inheritance. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #42 so that we can fix later.

},

__resetProperties: function() {
this._setActive(false);
this._matched = null;
//this.tail = { path: null, prefix: null, queryParams: null };
//this.data = {};
},

__tryToMatch: function(path, pattern) {
if (this._skipMatch || !path || !pattern) {
if (this._skipMatch || !pattern) {
return;
}

if (!path) {
this.__resetProperties();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

return;
}

var remainingPieces = path.split('/');
var patternPieces = pattern.split('/');

var matched = [];
var namedMatches = {};

var clearProperties = function clearProperties() {
this._setActive(false);
this._matched = null;
// this.tail = null;
// this.data = {};
}.bind(this);

for (var i=0; i < patternPieces.length; i++) {
var patternPiece = patternPieces[i];
if (!patternPiece && patternPiece !== '') {
Expand All @@ -186,15 +217,15 @@

// We don't match this path.
if (!pathPiece && pathPiece !== '') {
clearProperties();
this.__resetProperties();
return;
}
matched.push(pathPiece);

if (patternPiece.charAt(0) == ':') {
namedMatches[patternPiece.slice(1)] = pathPiece;
} else if (patternPiece !== pathPiece) {
clearProperties();
this.__resetProperties();
return;
}
}
Expand All @@ -214,7 +245,7 @@
this.tail = {
prefix: tailPrefix,
path: tailPath,
queryParams: this.route.queryParams
__queryParams: this.route.__queryParams
};
}
this._setActive(true);
Expand Down
3 changes: 1 addition & 2 deletions test/app-example-1.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
</carbon-route>
<carbon-route route='{{route}}' pattern='/user' tail='{{userRoute}}'>
</carbon-route>

<carbon-route route='{{userRoute}}' pattern='/:page' data='{{userData}}'>
<carbon-route route='{{userRoute}}' pattern='/:page' data='{{userData}}' query-params="{{userQueryParams}}">
</carbon-route>
</template>
<script>
Expand Down
12 changes: 6 additions & 6 deletions test/carbon-location.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@

setup(function() {
originalPath = carbonLocation.route.path;
originalQueryParams = assign({}, carbonLocation.route.queryParams);
originalQueryParams = assign({}, carbonLocation.route.__queryParams);
});

teardown(function() {
carbonLocation.set('route.prefix', '');
carbonLocation.set('route.path', originalPath);
carbonLocation.set('route.queryParams', originalQueryParams);
carbonLocation.set('route.__queryParams', originalQueryParams);
});

test('it reflects path to location.pathname', function() {
Expand All @@ -99,12 +99,12 @@
});

test('it reflects queryParams values to location.search', function() {
carbonLocation.set('route.queryParams.foo', 1);
carbonLocation.set('route.__queryParams.foo', 1);
expect(window.location.search).to.match(/foo=1/);
});

test('it reflects completely replaced queryParams', function() {
carbonLocation.set('route.queryParams', { bar: 1 });
carbonLocation.set('route.__queryParams', { bar: 1 });
expect(window.location.search).to.be.equal('?bar=1');
});

Expand Down Expand Up @@ -134,9 +134,9 @@
expect(carbonLocation.route.path).to.be.equal('/fiz/buz');
});

test('it reflects location.search to route.queryParams', function() {
test('it reflects location.search to route.__queryParams', function() {
setLocation('?fiz=buz');
expect(carbonLocation.route.queryParams).to.be.eql({
expect(carbonLocation.route.__queryParams).to.be.eql({
fiz: 'buz'
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/carbon-route-converter.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
expect(converter.route).to.be.deep.equal({
prefix: '',
path: '/a/b/c',
queryParams: queryParams
__queryParams: queryParams
});

converter.set('route.path', '/d/e/f');
expect(converter.path).to.be.equal('/d/e/f');

queryParams = {y: '11'};
converter.set('route.queryParams', queryParams);
converter.set('route.__queryParams', queryParams);
expect(converter.queryParams).to.be.deep.equal(queryParams);

queryParams['z'] = '12';
Expand Down
Loading