Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial QueryParams implementation #3182

Merged
merged 1 commit into from
Sep 10, 2013
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
59 changes: 57 additions & 2 deletions packages/ember-routing/lib/helpers/link_to.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,40 @@ Ember.onLoad('Ember.Handlebars', function(Handlebars) {
Ember.Handlebars.normalizePath(templateContext, path, helperParameters.options.data);
this.registerObserver(normalizedPath.root, normalizedPath.path, this, this._paramsChanged);
}


if (Ember.FEATURES.isEnabled("query-params")) {
var queryParams = get(this, '_potentialQueryParams') || [];

for(i=0; i < queryParams.length; i++) {
this.registerObserver(this, queryParams[i], this, this._queryParamsChanged);
}
}
},

/**
@private

This method is invoked by observers installed during `init` that fire
whenever the helpers
whenever the params change
@method _paramsChanged
*/
_paramsChanged: function() {
this.notifyPropertyChange('resolvedParams');
},


/**
@private

This method is invoked by observers installed during `init` that fire
whenever the query params change
*/
_queryParamsChanged: function (object, path) {
this.notifyPropertyChange('queryParams');
},


/**
@private

Expand Down Expand Up @@ -345,7 +366,6 @@ Ember.onLoad('Ember.Handlebars', function(Handlebars) {
@return {Array} An array with the route name and any dynamic segments
*/
routeArgs: Ember.computed(function() {

var resolvedParams = get(this, 'resolvedParams').slice(0),
router = get(this, 'router'),
namedRoute = resolvedParams[0];
Expand All @@ -365,9 +385,44 @@ Ember.onLoad('Ember.Handlebars', function(Handlebars) {
}
}

if (Ember.FEATURES.isEnabled("query-params")) {
var queryParams = get(this, 'queryParams');

if (queryParams || queryParams === false) { resolvedParams.push({queryParams: queryParams}); }
}

return resolvedParams;
}).property('resolvedParams', 'queryParams', 'router.url'),


_potentialQueryParams: Ember.computed(function () {
var namedRoute = get(this, 'resolvedParams')[0];
if (!namedRoute) { return null; }
var router = get(this, 'router');

namedRoute = fullRouteName(router, namedRoute);

return router.router.queryParamsForHandler(namedRoute);
}).property('resolvedParams'),

queryParams: Ember.computed(function () {
var self = this,
queryParams = null,
allowedQueryParams = get(this, '_potentialQueryParams');

if (!allowedQueryParams) { return null; }
allowedQueryParams.forEach(function (param) {
var value = get(self, param);
if (typeof value !== 'undefined') {
queryParams = queryParams || {};
queryParams[param] = value;
}
});


return queryParams;
}).property('_potentialQueryParams.[]'),

/**
Sets the element's `href` attribute to the url for
the `LinkView`'s targeted route.
Expand Down
17 changes: 11 additions & 6 deletions packages/ember-routing/lib/system/dsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ DSL.prototype = {
if (callback) {
var dsl = new DSL(name);
callback.call(dsl);
this.push(options.path, name, dsl.generate());
this.push(options.path, name, dsl.generate(), options.queryParams);
} else {
this.push(options.path, name);
this.push(options.path, name, null, options.queryParams);
}
},

push: function(url, name, callback) {
push: function(url, name, callback, queryParams) {
var parts = name.split('.');
if (url === "" || url === "/" || parts[parts.length-1] === "index") { this.explicitIndex = true; }

this.matches.push([url, name, callback]);
this.matches.push([url, name, callback, queryParams]);
},

route: function(name, options) {
Expand All @@ -52,7 +52,7 @@ DSL.prototype = {
name = this.parent + "." + name;
}

this.push(options.path, name);
this.push(options.path, name, null, options.queryParams);
},

generate: function() {
Expand All @@ -65,7 +65,12 @@ DSL.prototype = {
return function(match) {
for (var i=0, l=dslMatches.length; i<l; i++) {
var dslMatch = dslMatches[i];
match(dslMatch[0]).to(dslMatch[1], dslMatch[2]);
var matchObj = match(dslMatch[0]).to(dslMatch[1], dslMatch[2]);
if (Ember.FEATURES.isEnabled("query-params")) {
if(dslMatch[3]) {
matchObj.withQueryParams.apply(matchObj, dslMatch[3]);
}
}
}
};
}
Expand Down
17 changes: 13 additions & 4 deletions packages/ember-routing/lib/system/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ Ember.Route = Ember.Object.extend(Ember.ActionHandler, {

@method setup
*/
setup: function(context) {
setup: function(context, queryParams) {
var controllerName = this.controllerName || this.routeName,
controller = this.controllerFor(controllerName, true);
if (!controller) {
Expand All @@ -400,18 +400,24 @@ Ember.Route = Ember.Object.extend(Ember.ActionHandler, {
// referenced in action handlers
this.controller = controller;

var args = [controller, context];

if (Ember.FEATURES.isEnabled("query-params")) {
args.push(queryParams);
}

if (this.setupControllers) {
Ember.deprecate("Ember.Route.setupControllers is deprecated. Please use Ember.Route.setupController(controller, model) instead.");
this.setupControllers(controller, context);
} else {
this.setupController(controller, context);
this.setupController.apply(this, args);
}

if (this.renderTemplates) {
Ember.deprecate("Ember.Route.renderTemplates is deprecated. Please use Ember.Route.renderTemplate(controller, model) instead.");
this.renderTemplates(context);
} else {
this.renderTemplate(controller, context);
this.renderTemplate.apply(this, args);
}
},

Expand Down Expand Up @@ -502,6 +508,7 @@ Ember.Route = Ember.Object.extend(Ember.ActionHandler, {

@method beforeModel
@param {Transition} transition
@param {Object} queryParams the active query params for this route
@return {Promise} if the value returned from this hook is
a promise, the transition will pause until the transition
resolves. Otherwise, non-promise return values are not
Expand Down Expand Up @@ -535,12 +542,13 @@ Ember.Route = Ember.Object.extend(Ember.ActionHandler, {
@param {Object} resolvedModel the value returned from `model`,
or its resolved value if it was a promise
@param {Transition} transition
@param {Object} queryParams the active query params for this handler
@return {Promise} if the value returned from this hook is
a promise, the transition will pause until the transition
resolves. Otherwise, non-promise return values are not
utilized in any way.
*/
afterModel: function(resolvedModel, transition) {
afterModel: function(resolvedModel, transition, queryParams) {
this.redirect(resolvedModel, transition);
},

Expand Down Expand Up @@ -600,6 +608,7 @@ Ember.Route = Ember.Object.extend(Ember.ActionHandler, {
@method model
@param {Object} params the parameters extracted from the URL
@param {Transition} transition
@param {Object} queryParams the query params for this route
@return {Object|Promise} the model for this route. If
a promise is returned, the transition will pause until
the promise resolves, and the resolved value of the promise
Expand Down
12 changes: 8 additions & 4 deletions packages/ember-routing/lib/system/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,16 @@ Ember.Router = Ember.Object.extend({
args = [].slice.call(args);
args[0] = args[0] || '/';

var passedName = args[0], name, self = this;
var passedName = args[0], name, self = this,
isQueryParamsOnly = false;

if (passedName.charAt(0) === '/') {
name = passedName;
} else {
if (Ember.FEATURES.isEnabled("query-params")) {
isQueryParamsOnly = (args.length === 1 && args[0].hasOwnProperty('queryParams'));
}

if (!isQueryParamsOnly && passedName.charAt(0) === '/') {
name = passedName;
} else if (!isQueryParamsOnly) {
if (!this.router.hasRoute(passedName)) {
name = args[0] = passedName + '.index';
} else {
Expand Down
Loading