Skip to content

Commit 9308d55

Browse files
committed
[FEATURE query-params-new] Query params rewrite
See here for some examples: https://gist.github.com/machty/8167051 Depends on Handlebars subexpressions: handlebars-lang/handlebars.js#690
1 parent dc0dc0c commit 9308d55

File tree

18 files changed

+2382
-1701
lines changed

18 files changed

+2382
-1701
lines changed

FEATURES.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@ for a detailed explanation.
2121

2222
Added in [#3614](https://github.com/emberjs/ember.js/pull/3614).
2323

24-
* `query-params`
25-
26-
Add query params support to the ember router. You can now define which query
27-
params your routes respond to, use them in your route hooks to affect model
28-
loading or controller state, and transition query parameters with the link-to
29-
helper and the transitionTo method.
30-
31-
Added in [#3182](https://github.com/emberjs/ember.js/pull/3182).
3224
* `propertyBraceExpansion`
3325

3426
Adds support for brace-expansion in dependent keys, observer, and watch properties.
@@ -129,3 +121,12 @@ for a detailed explanation.
129121
Ember.computed.oneWay('foo').readOnly().
130122

131123
Added in [#3879](https://github.com/emberjs/ember.js/pull/3879)
124+
125+
* `query-params-new`
126+
127+
Add query params support to the ember router. This is a rewrite of a
128+
previous attempt at an API for query params. You can define query
129+
param properties on route-driven controllers with the `queryParams`
130+
property, and any changes to those properties will cause the URL
131+
to update, and in the other direction, any URL changes to the query
132+
params will cause those controller properties to update.

Gemfile.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ PATH
2828
remote: .
2929
specs:
3030
ember-source (1.4.0.beta.1.canary)
31-
handlebars-source (~> 1.1.2)
31+
handlebars-source (~> 1.2.0)
3232

3333
GEM
3434
remote: https://rubygems.org/
@@ -45,7 +45,7 @@ GEM
4545
diff-lcs (~> 1.1)
4646
mime-types (~> 1.15)
4747
posix-spawn (~> 0.3.6)
48-
handlebars-source (1.1.2)
48+
handlebars-source (1.2.1)
4949
json (1.8.1)
5050
kicker (3.0.0)
5151
listen (~> 1.3.0)

ember-source.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Gem::Specification.new do |gem|
1313

1414
gem.version = Ember.rubygems_version_string
1515

16-
gem.add_dependency "handlebars-source", ["~> 1.1.2"]
16+
gem.add_dependency "handlebars-source", ["~> 1.2.0"]
1717

1818
gem.files = %w(VERSION) + Dir['dist/*.js', 'lib/ember/*.rb']
1919
end

features.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"reduceComputed-non-array-dependencies": true,
33
"ember-testing-lazy-routing": true,
44
"ember-testing-wait-hooks": true,
5-
"query-params": null,
5+
"query-params-new": null,
66
"string-humanize": null,
77
"string-parameterize": null,
88
"propertyBraceExpansion": null,

packages/ember-handlebars-compiler/lib/main.js

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ var DOT_LOOKUP_REGEX = /helpers\.(.*?)\)/,
191191
INVOCATION_SPLITTING_REGEX = /(.*blockHelperMissing\.call\(.*)(stack[0-9]+)(,.*)/;
192192

193193
Ember.Handlebars.JavaScriptCompiler.stringifyLastBlockHelperMissingInvocation = function(source) {
194+
debugger;
194195
var helperInvocation = source[source.length - 1],
195196
helperName = (DOT_LOOKUP_REGEX.exec(helperInvocation) || BRACKET_STRING_LOOKUP_REGEX.exec(helperInvocation))[1],
196197
matches = INVOCATION_SPLITTING_REGEX.exec(helperInvocation);

packages/ember-routing/lib/ext/controller.js

+87-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
@submodule ember-routing
44
*/
55

6-
var get = Ember.get, set = Ember.set;
6+
var get = Ember.get, set = Ember.set,
7+
map = Ember.EnumerableUtils.map;
8+
9+
var queuedQueryParamChanges = {};
710

811
Ember.ControllerMixin.reopen({
912
/**
@@ -61,7 +64,7 @@ Ember.ControllerMixin.reopen({
6164

6265
/**
6366
Transition into another route while replacing the current URL, if possible.
64-
This will replace the current history entry instead of adding a new one.
67+
This will replace the current history entry instead of adding a new one.
6568
Beside that, it is identical to `transitionToRoute` in all other respects.
6669
6770
```javascript
@@ -111,3 +114,85 @@ Ember.ControllerMixin.reopen({
111114
return this.replaceRoute.apply(this, arguments);
112115
}
113116
});
117+
118+
if (Ember.FEATURES.isEnabled("query-params-new")) {
119+
Ember.ControllerMixin.reopen({
120+
121+
concatenatedProperties: ['queryParams'],
122+
123+
queryParams: null,
124+
125+
_queryParamScope: null,
126+
127+
_finalizingQueryParams: false,
128+
_queryParamHash: Ember.computed(function computeQueryParamHash() {
129+
130+
// Given: queryParams: ['foo', 'bar:baz'] on controller:thing
131+
// _queryParamHash should yield: { 'foo': 'thing[foo]' }
132+
133+
var result = {};
134+
var queryParams = this.queryParams;
135+
if (!queryParams) {
136+
return result;
137+
}
138+
139+
for (var i = 0, len = queryParams.length; i < len; ++i) {
140+
var full = queryParams[i];
141+
var parts = full.split(':');
142+
var key = parts[0];
143+
var urlKey = parts[1];
144+
if (!urlKey) {
145+
if (this._queryParamScope) {
146+
urlKey = this._queryParamScope + '[' + key + ']';
147+
} else {
148+
urlKey = key;
149+
}
150+
}
151+
result[key] = urlKey;
152+
}
153+
154+
return result;
155+
}),
156+
157+
_activateQueryParamObservers: function() {
158+
var queryParams = get(this, '_queryParamHash');
159+
160+
for (var k in queryParams) {
161+
if (queryParams.hasOwnProperty(k)) {
162+
this.addObserver(k, this, this._queryParamChanged);
163+
}
164+
}
165+
},
166+
167+
_deactivateQueryParamObservers: function() {
168+
var queryParams = get(this, '_queryParamHash');
169+
170+
for (var k in queryParams) {
171+
if (queryParams.hasOwnProperty(k)) {
172+
this.removeObserver(k, this, this._queryParamChanged);
173+
}
174+
}
175+
},
176+
177+
_queryParamChanged: function(controller, key) {
178+
if (this._finalizingQueryParams) {
179+
var changes = this._queryParamChangesDuringSuspension;
180+
if (changes) {
181+
changes[key] = true;
182+
}
183+
return;
184+
}
185+
186+
var queryParams = get(this, '_queryParamHash');
187+
queuedQueryParamChanges[queryParams[key]] = get(this, key);
188+
Ember.run.once(this, this._fireQueryParamTransition);
189+
},
190+
191+
_fireQueryParamTransition: function() {
192+
this.transitionToRoute({ queryParams: queuedQueryParamChanges });
193+
queuedQueryParamChanges = {};
194+
},
195+
196+
_queryParamChangesDuringSuspension: null
197+
});
198+
}

0 commit comments

Comments
 (0)