-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
setting a controller property in setupController
does not update the query param
#5465
Comments
Fixed with a @machty - What is the expected behavior here? |
I thought we had explicit tests for this... checking. |
I think this is a bug... we have tests for this but only in the context of a working: http://jsbin.com/mejeto/1#/about |
Failing test for setting a controller property in `setupController` does not update the query param
I have added a failing test for this bug #5473 |
I think I stumbled upon related issue, so I'm not sure if it needs separate ticket here in GitHub. export default Ember.Controller.extend({
queryParams: ['modal'],
modal: null,
handleModal: function () {
this.send('openModal', this.get('modal'));
}.observes('modal')
}); export default Ember.Route.extend({
actions: {
openModal: function (name) {
return this.render(name, {
into: 'application',
outlet: 'modal'
});
}
}
}); This works in all cases besides the bootup when it throws Can be fixed using handleModal: function () {
Ember.run.next(this, function () {
this.send('openModal', this.get('modal'));
});
}.observes('modal') though it feels wrong. JSBins: |
@igor10k I'm running into this exact same issue, modal and all. Wrapping it in Ember.run.next fixes it, but it feels wrong to me as well. |
Can anyone confirm that this is still an issue in 1.8? |
@wagenet yes this is still an issue in 1.8: and canary: |
@machty - Thoughts? |
I think this is also a bug for me too using 1.8.1. What I want to do is call a special method on my controller every time a specific queryParam changes. I have a router that looks something like this.
|
@machty - Ideas on where to start digging on this? |
I'm having the same problem. this.set() in the controller will not update query parameter in the URL |
App.SearchController = Ember.Controller.extend({ domainField: Ember.computed.oneWay('domain'), App.SearchRoute = Ember.Route.extend({ },
}, |
Can you please provide a work around in the interim? |
To clarify: this.set() in my code will make the change to the Class object, but doesn't change the URL at all |
This is ancient, but seems like it’s still a problem? I can’t figure out the circumstances, but periodically when I try to set a query parameter from the controller, it doesn’t propagate to the URL. I can check the value of the property on the controller and it’s correctly set, but the URL hasn’t changed. I tried using an |
Experiencing this issue too |
@rwjblue: Is there any update on this? |
Any update? Here even when changing the value from the controller, the url updates for a couple ms and it goes back to the old value, even if the controller has the new value in the variable |
Here's a workaround: In the controller, add an observer that changes the query param in the next run loop after it has been set. In this example, I wanted to remove the import Ember from 'ember';
export default Ember.Controller.extend({
queryParams: ['token'],
token: null,
unsetToken: Ember.observer('token', function() {
if (this.get('token')) {
Ember.run.next(this, function() {
this.set('token', null);
});
}
}),
}); |
Just ran into this today, when I tried to set a query parameter from the setupController on boot. run.next seems like a workaround, but i think proper solution would be to have 2 routes and on boot first route would do transitionTo second route with the query parameter value. Ember 2.8 |
Marking issue a inactive as per our triage policy. |
I too am seeing this issue on Ember 2.11, trying to clear a query param in the controller has no effect, @barelyknown 's workaround gets me most of the way there but I would still consider this an open bug. |
EDIT: Ignore all this jazz. There's some bad/wrong information here, and @locks pointed me towards a much better solution—with a query parameter instead of a dynamic segment—in-line immediately below. It binds the query parameter to a normal property with a shadow computed property that sanitizes and sets on The better solution...// app/router.js
Router.map(function() {
this.route('shop');
});
// app/routes/shop.js
export default Ember.Route.extend({
queryParams: {
_selectedItemIndex: {
replace: true
}
}
});
// app/controllers/shop.js
import computed from 'ember-macro-helpers/computed';
export default Ember.Controller.extend({
cart: Ember.service.inject(),
queryParams: {
_selectedItemIndex: 'i'
},
_selectedItemIndex: 0,
selectedItemIndex: computed('_selectedItemIndex', {
get(index) {
const itemsLength = this.get('cart.items.length');
if (isNaN(index) || index < 0 || index >= itemsLength) {
index = 0;
}
return this.set('_selectedItemIndex', index);
},
set(index) {
return this.set('_selectedItemIndex', index);
}
})
}); The original problem and solution...I lost the better part of yesterday to this issue that still persists in 2.14-beta. My use case is sanitizing a query parameter to use as an index into an Ember.Array. If it's malformed (e.g. NaN or a string) and/or out of bounds, it should use a default index, and update the URL to reflect the adjustment (so that events in response to future clicks trigger appropriately—I can explain this further to help make the case that this is not merely a cosmetic issue). I can't set the property from I can't use a computed property with a sanitizing setter in the controller because you can't bind a query parameter to a computed property. I can't use an observer on a normal property because you can't set the property you're observing. I can't use an observer with Ember.run due to the issue described above. I can't call Clearly there is a nexus of pain here. This issue, issue #14606, and the limitation of not being able to bind a query parameter to a computed property all combine to create a neat, frustrating little trap. Here's my workaround: I created a nested "select" route that simply takes the query parameter (or—as in the code below—a dynamic segment), sanitizes it, and sets the property on its parent controller. If the sanitized index is out of bounds it transitions to the nested index route, which in turn transitions back to the nested select route with the default index. The nested routes have no templates or controllers and the parent route has no outlet; they exist solely to handle this routing issue. // app/router.js
Router.map(function() {
this.route('shop', function() {
this.route('index', { path: '/' });
this.route('select', { path: '/:index' });
});
});
// app/routes/shop.js
export default Ember.Route.extend({
setupController(controller, model) {
this._super(...arguments);
// some logic to set up the cart service
},
model() {
// fetch products to shop
}
});
// app/controllers/shop.js
export default Ember.Controller.extend({
cart: Ember.service.inject(),
selectedItemIndex: 0,
actions: {
selectItem(index) {
return this.replaceRoute('shop.select', index);
}
}
});
// app/routes/shop/index.js
export default Ember.Route.extend({
beforeModel() {
return this.replaceWith('shop.select', 0);
}
});
// app/routes/shop/select.js
export default Ember.Route.extend({
setupController(_, { index }) {
this._super(...arguments);
const
parentController = this.controllerFor('shop'),
itemsLength = parentController.get('cart.items.length');
index = parseInt(index, 10);
if (Number.isNaN(index) || index < 0 || index >= itemsLength) {
return this.replaceWith('shop.index');
}
parentController.set('selectedItemIndex', index);
},
model: _ => _
}); |
@mwpastore thanks for the update we'll close this out. Have a great weekend! |
@locks if you want please reopen a documentation issue to note this on the |
@mwpastore Sounds like we could follow up with a post on the forum on https://discuss.emberjs.com/ with your solution |
Wait so this issue got closed with an alternate solution to work around the bug it reports, and that solution involves setting a property in a computed getter? |
I'm curious if there's update on this. I'm using |
if you have a Controller defined as
and in the Routes setupController you do something like...
The query param does not get set in the url.
You can see a working bin here: http://emberjs.jsbin.com/dekuj/1/edit
The text was updated successfully, but these errors were encountered: