Ember: Even after refresh some attributes in the model are not changed
I am new to EmberJS, and facing problem with model data updation
Controller :
export default Ember.Controller.extend({
queryParams: ['jobid'],
jobid: null,
currentCount: null,
actions: {
onOffToggle(e) {
var isChanged = this.get('model.b.isChanged');
this.set('model.b.enable', e.target.checked);
this.set('model.b.isChanged', !isChanged);
console.log(this.get('model.b.isChanged'))
},
iterationCountChange() {
var currentCount = this.get('currentCount');
var isCountChanged =
(currentCount != this.get('model.b.count')) ? true : false;
this.set('model.b.isCountChanged', isCountChanged);
}
}});
Route:
export default Ember.Route.extend({
ajax: Ember.inject.service(),
beforeModel: function (transition) {
this.jobid = transition.queryParams.jobid;
},
model(){
return Ember.RSVP.hash({
a: this.store.queryRecord('a', {jobid: this.get("jobid")}),
b: this.store.queryRecord('b', {id: this.get("jobid")})
});
},
setupController: function(controller, model) {
this._super(controller, model)
controller.set('currentCount', model.b.get('iterationCount'))
},
actions: {
paramChange(a, b)
Ember.$.ajax({
url: "/rest/test",
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
b: b,
a: a
})
}).then(response => {
this.refresh();
})
},
error(error, transition) {
if (error.errors[0].status == 404) {
return this.transitionTo('not-found', { queryParams: {'previous': window.location.href}});
}
}
}
});
Here in controller I am keeping track if some value have changed, and if they have changed then update the flag related to their change, these flags like isChanged
and isCountChanged
are the part of the model's data, after user cliks submit button , paramChange
action is called and then a post call is made to update the db for respective property changes, and then this.refresh()
is called to render the latest model data.
But the problem is, once isChanged
and/or isCountChanged
are changed from their default value, then they don't reset to the new value present in the model data, e.g. after refresh
the value to both these flags should be reset to false but it comes always true, I checked the value in the setUpController
hook for the values of these flags and it confirms to true.
According to me it has something to with the controller, since any value which is used in controller once is not resetting to new value coming after refresh
.
Kindly help I am spent a lot of time in this and got nothing till now, do inform if any extra information is required.
Ember version: 2.6
javascript html ajax model-view-controller ember.js
|
show 3 more comments
I am new to EmberJS, and facing problem with model data updation
Controller :
export default Ember.Controller.extend({
queryParams: ['jobid'],
jobid: null,
currentCount: null,
actions: {
onOffToggle(e) {
var isChanged = this.get('model.b.isChanged');
this.set('model.b.enable', e.target.checked);
this.set('model.b.isChanged', !isChanged);
console.log(this.get('model.b.isChanged'))
},
iterationCountChange() {
var currentCount = this.get('currentCount');
var isCountChanged =
(currentCount != this.get('model.b.count')) ? true : false;
this.set('model.b.isCountChanged', isCountChanged);
}
}});
Route:
export default Ember.Route.extend({
ajax: Ember.inject.service(),
beforeModel: function (transition) {
this.jobid = transition.queryParams.jobid;
},
model(){
return Ember.RSVP.hash({
a: this.store.queryRecord('a', {jobid: this.get("jobid")}),
b: this.store.queryRecord('b', {id: this.get("jobid")})
});
},
setupController: function(controller, model) {
this._super(controller, model)
controller.set('currentCount', model.b.get('iterationCount'))
},
actions: {
paramChange(a, b)
Ember.$.ajax({
url: "/rest/test",
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
b: b,
a: a
})
}).then(response => {
this.refresh();
})
},
error(error, transition) {
if (error.errors[0].status == 404) {
return this.transitionTo('not-found', { queryParams: {'previous': window.location.href}});
}
}
}
});
Here in controller I am keeping track if some value have changed, and if they have changed then update the flag related to their change, these flags like isChanged
and isCountChanged
are the part of the model's data, after user cliks submit button , paramChange
action is called and then a post call is made to update the db for respective property changes, and then this.refresh()
is called to render the latest model data.
But the problem is, once isChanged
and/or isCountChanged
are changed from their default value, then they don't reset to the new value present in the model data, e.g. after refresh
the value to both these flags should be reset to false but it comes always true, I checked the value in the setUpController
hook for the values of these flags and it confirms to true.
According to me it has something to with the controller, since any value which is used in controller once is not resetting to new value coming after refresh
.
Kindly help I am spent a lot of time in this and got nothing till now, do inform if any extra information is required.
Ember version: 2.6
javascript html ajax model-view-controller ember.js
Even the value ofcurrentCount
is not resetting, which is not updated in backend, all this is leading to update the backed as the flags for tracking updation are always true once a modification is made.
– JSR29
Nov 22 '18 at 15:22
In the model hook , I must be getting updated as in the Inspect the API call response shows the new data is received, I don't know to print the model details in model hook.
– JSR29
Nov 22 '18 at 15:24
controllers are singletons so their state is not reset during your application lifecycle. If you want this use a component or clean up yourself insetupController
.
– Lux
Nov 22 '18 at 18:09
How using a component would help? Also it is okay to set the variables of controller to set, but my biggest concern is that why my model attributes are not refreshing.
– JSR29
Nov 23 '18 at 7:58
How are you initializingisChanged
andisCountChanged
? You claim these "are the part of the model's data". Are you storing these values in the database? If you set adebugger
in thesetupController
, is your model data as you expect it? What ismodel.tunein
?
– mistahenry
Nov 23 '18 at 10:05
|
show 3 more comments
I am new to EmberJS, and facing problem with model data updation
Controller :
export default Ember.Controller.extend({
queryParams: ['jobid'],
jobid: null,
currentCount: null,
actions: {
onOffToggle(e) {
var isChanged = this.get('model.b.isChanged');
this.set('model.b.enable', e.target.checked);
this.set('model.b.isChanged', !isChanged);
console.log(this.get('model.b.isChanged'))
},
iterationCountChange() {
var currentCount = this.get('currentCount');
var isCountChanged =
(currentCount != this.get('model.b.count')) ? true : false;
this.set('model.b.isCountChanged', isCountChanged);
}
}});
Route:
export default Ember.Route.extend({
ajax: Ember.inject.service(),
beforeModel: function (transition) {
this.jobid = transition.queryParams.jobid;
},
model(){
return Ember.RSVP.hash({
a: this.store.queryRecord('a', {jobid: this.get("jobid")}),
b: this.store.queryRecord('b', {id: this.get("jobid")})
});
},
setupController: function(controller, model) {
this._super(controller, model)
controller.set('currentCount', model.b.get('iterationCount'))
},
actions: {
paramChange(a, b)
Ember.$.ajax({
url: "/rest/test",
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
b: b,
a: a
})
}).then(response => {
this.refresh();
})
},
error(error, transition) {
if (error.errors[0].status == 404) {
return this.transitionTo('not-found', { queryParams: {'previous': window.location.href}});
}
}
}
});
Here in controller I am keeping track if some value have changed, and if they have changed then update the flag related to their change, these flags like isChanged
and isCountChanged
are the part of the model's data, after user cliks submit button , paramChange
action is called and then a post call is made to update the db for respective property changes, and then this.refresh()
is called to render the latest model data.
But the problem is, once isChanged
and/or isCountChanged
are changed from their default value, then they don't reset to the new value present in the model data, e.g. after refresh
the value to both these flags should be reset to false but it comes always true, I checked the value in the setUpController
hook for the values of these flags and it confirms to true.
According to me it has something to with the controller, since any value which is used in controller once is not resetting to new value coming after refresh
.
Kindly help I am spent a lot of time in this and got nothing till now, do inform if any extra information is required.
Ember version: 2.6
javascript html ajax model-view-controller ember.js
I am new to EmberJS, and facing problem with model data updation
Controller :
export default Ember.Controller.extend({
queryParams: ['jobid'],
jobid: null,
currentCount: null,
actions: {
onOffToggle(e) {
var isChanged = this.get('model.b.isChanged');
this.set('model.b.enable', e.target.checked);
this.set('model.b.isChanged', !isChanged);
console.log(this.get('model.b.isChanged'))
},
iterationCountChange() {
var currentCount = this.get('currentCount');
var isCountChanged =
(currentCount != this.get('model.b.count')) ? true : false;
this.set('model.b.isCountChanged', isCountChanged);
}
}});
Route:
export default Ember.Route.extend({
ajax: Ember.inject.service(),
beforeModel: function (transition) {
this.jobid = transition.queryParams.jobid;
},
model(){
return Ember.RSVP.hash({
a: this.store.queryRecord('a', {jobid: this.get("jobid")}),
b: this.store.queryRecord('b', {id: this.get("jobid")})
});
},
setupController: function(controller, model) {
this._super(controller, model)
controller.set('currentCount', model.b.get('iterationCount'))
},
actions: {
paramChange(a, b)
Ember.$.ajax({
url: "/rest/test",
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
b: b,
a: a
})
}).then(response => {
this.refresh();
})
},
error(error, transition) {
if (error.errors[0].status == 404) {
return this.transitionTo('not-found', { queryParams: {'previous': window.location.href}});
}
}
}
});
Here in controller I am keeping track if some value have changed, and if they have changed then update the flag related to their change, these flags like isChanged
and isCountChanged
are the part of the model's data, after user cliks submit button , paramChange
action is called and then a post call is made to update the db for respective property changes, and then this.refresh()
is called to render the latest model data.
But the problem is, once isChanged
and/or isCountChanged
are changed from their default value, then they don't reset to the new value present in the model data, e.g. after refresh
the value to both these flags should be reset to false but it comes always true, I checked the value in the setUpController
hook for the values of these flags and it confirms to true.
According to me it has something to with the controller, since any value which is used in controller once is not resetting to new value coming after refresh
.
Kindly help I am spent a lot of time in this and got nothing till now, do inform if any extra information is required.
Ember version: 2.6
javascript html ajax model-view-controller ember.js
javascript html ajax model-view-controller ember.js
edited Nov 26 '18 at 6:39
JSR29
asked Nov 22 '18 at 15:19
JSR29JSR29
151212
151212
Even the value ofcurrentCount
is not resetting, which is not updated in backend, all this is leading to update the backed as the flags for tracking updation are always true once a modification is made.
– JSR29
Nov 22 '18 at 15:22
In the model hook , I must be getting updated as in the Inspect the API call response shows the new data is received, I don't know to print the model details in model hook.
– JSR29
Nov 22 '18 at 15:24
controllers are singletons so their state is not reset during your application lifecycle. If you want this use a component or clean up yourself insetupController
.
– Lux
Nov 22 '18 at 18:09
How using a component would help? Also it is okay to set the variables of controller to set, but my biggest concern is that why my model attributes are not refreshing.
– JSR29
Nov 23 '18 at 7:58
How are you initializingisChanged
andisCountChanged
? You claim these "are the part of the model's data". Are you storing these values in the database? If you set adebugger
in thesetupController
, is your model data as you expect it? What ismodel.tunein
?
– mistahenry
Nov 23 '18 at 10:05
|
show 3 more comments
Even the value ofcurrentCount
is not resetting, which is not updated in backend, all this is leading to update the backed as the flags for tracking updation are always true once a modification is made.
– JSR29
Nov 22 '18 at 15:22
In the model hook , I must be getting updated as in the Inspect the API call response shows the new data is received, I don't know to print the model details in model hook.
– JSR29
Nov 22 '18 at 15:24
controllers are singletons so their state is not reset during your application lifecycle. If you want this use a component or clean up yourself insetupController
.
– Lux
Nov 22 '18 at 18:09
How using a component would help? Also it is okay to set the variables of controller to set, but my biggest concern is that why my model attributes are not refreshing.
– JSR29
Nov 23 '18 at 7:58
How are you initializingisChanged
andisCountChanged
? You claim these "are the part of the model's data". Are you storing these values in the database? If you set adebugger
in thesetupController
, is your model data as you expect it? What ismodel.tunein
?
– mistahenry
Nov 23 '18 at 10:05
Even the value of
currentCount
is not resetting, which is not updated in backend, all this is leading to update the backed as the flags for tracking updation are always true once a modification is made.– JSR29
Nov 22 '18 at 15:22
Even the value of
currentCount
is not resetting, which is not updated in backend, all this is leading to update the backed as the flags for tracking updation are always true once a modification is made.– JSR29
Nov 22 '18 at 15:22
In the model hook , I must be getting updated as in the Inspect the API call response shows the new data is received, I don't know to print the model details in model hook.
– JSR29
Nov 22 '18 at 15:24
In the model hook , I must be getting updated as in the Inspect the API call response shows the new data is received, I don't know to print the model details in model hook.
– JSR29
Nov 22 '18 at 15:24
controllers are singletons so their state is not reset during your application lifecycle. If you want this use a component or clean up yourself in
setupController
.– Lux
Nov 22 '18 at 18:09
controllers are singletons so their state is not reset during your application lifecycle. If you want this use a component or clean up yourself in
setupController
.– Lux
Nov 22 '18 at 18:09
How using a component would help? Also it is okay to set the variables of controller to set, but my biggest concern is that why my model attributes are not refreshing.
– JSR29
Nov 23 '18 at 7:58
How using a component would help? Also it is okay to set the variables of controller to set, but my biggest concern is that why my model attributes are not refreshing.
– JSR29
Nov 23 '18 at 7:58
How are you initializing
isChanged
and isCountChanged
? You claim these "are the part of the model's data". Are you storing these values in the database? If you set a debugger
in the setupController
, is your model data as you expect it? What is model.tunein
?– mistahenry
Nov 23 '18 at 10:05
How are you initializing
isChanged
and isCountChanged
? You claim these "are the part of the model's data". Are you storing these values in the database? If you set a debugger
in the setupController
, is your model data as you expect it? What is model.tunein
?– mistahenry
Nov 23 '18 at 10:05
|
show 3 more comments
1 Answer
1
active
oldest
votes
From docs,
Refresh the model on this route and any child routes, firing the
beforeModel, model, and afterModel hooks in a similar fashion to how
routes are entered when transitioning in from other route. The current
route params (e.g. article_id) will be passed in to the respective
model hooks, and if a different model is returned, setupController and
associated route hooks will re-fire as well.
So, if your model data doesn't change, setupController()
is not called. My approach is to have a custom method to update controller with model data. Then, I call this method from model()
hook (for this.refresh()
) and from setupController()
.
model() {
return this.store.query(....).then(data => this.updateControllerData(data));
}
setupController(controller, model) {
this._super(...arguments);
this.updateControllerData(data);
}
updateControllerData(data = {}) {
if (!this.controller) {
return;
}
this.controller.setProperties(data);
}
Note that if setupController()
is not fired, the controller data is always updated.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433981%2fember-even-after-refresh-some-attributes-in-the-model-are-not-changed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
From docs,
Refresh the model on this route and any child routes, firing the
beforeModel, model, and afterModel hooks in a similar fashion to how
routes are entered when transitioning in from other route. The current
route params (e.g. article_id) will be passed in to the respective
model hooks, and if a different model is returned, setupController and
associated route hooks will re-fire as well.
So, if your model data doesn't change, setupController()
is not called. My approach is to have a custom method to update controller with model data. Then, I call this method from model()
hook (for this.refresh()
) and from setupController()
.
model() {
return this.store.query(....).then(data => this.updateControllerData(data));
}
setupController(controller, model) {
this._super(...arguments);
this.updateControllerData(data);
}
updateControllerData(data = {}) {
if (!this.controller) {
return;
}
this.controller.setProperties(data);
}
Note that if setupController()
is not fired, the controller data is always updated.
add a comment |
From docs,
Refresh the model on this route and any child routes, firing the
beforeModel, model, and afterModel hooks in a similar fashion to how
routes are entered when transitioning in from other route. The current
route params (e.g. article_id) will be passed in to the respective
model hooks, and if a different model is returned, setupController and
associated route hooks will re-fire as well.
So, if your model data doesn't change, setupController()
is not called. My approach is to have a custom method to update controller with model data. Then, I call this method from model()
hook (for this.refresh()
) and from setupController()
.
model() {
return this.store.query(....).then(data => this.updateControllerData(data));
}
setupController(controller, model) {
this._super(...arguments);
this.updateControllerData(data);
}
updateControllerData(data = {}) {
if (!this.controller) {
return;
}
this.controller.setProperties(data);
}
Note that if setupController()
is not fired, the controller data is always updated.
add a comment |
From docs,
Refresh the model on this route and any child routes, firing the
beforeModel, model, and afterModel hooks in a similar fashion to how
routes are entered when transitioning in from other route. The current
route params (e.g. article_id) will be passed in to the respective
model hooks, and if a different model is returned, setupController and
associated route hooks will re-fire as well.
So, if your model data doesn't change, setupController()
is not called. My approach is to have a custom method to update controller with model data. Then, I call this method from model()
hook (for this.refresh()
) and from setupController()
.
model() {
return this.store.query(....).then(data => this.updateControllerData(data));
}
setupController(controller, model) {
this._super(...arguments);
this.updateControllerData(data);
}
updateControllerData(data = {}) {
if (!this.controller) {
return;
}
this.controller.setProperties(data);
}
Note that if setupController()
is not fired, the controller data is always updated.
From docs,
Refresh the model on this route and any child routes, firing the
beforeModel, model, and afterModel hooks in a similar fashion to how
routes are entered when transitioning in from other route. The current
route params (e.g. article_id) will be passed in to the respective
model hooks, and if a different model is returned, setupController and
associated route hooks will re-fire as well.
So, if your model data doesn't change, setupController()
is not called. My approach is to have a custom method to update controller with model data. Then, I call this method from model()
hook (for this.refresh()
) and from setupController()
.
model() {
return this.store.query(....).then(data => this.updateControllerData(data));
}
setupController(controller, model) {
this._super(...arguments);
this.updateControllerData(data);
}
updateControllerData(data = {}) {
if (!this.controller) {
return;
}
this.controller.setProperties(data);
}
Note that if setupController()
is not fired, the controller data is always updated.
answered Nov 23 '18 at 22:37
Cesar Anibal Luis AlvaradoCesar Anibal Luis Alvarado
814
814
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433981%2fember-even-after-refresh-some-attributes-in-the-model-are-not-changed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Even the value of
currentCount
is not resetting, which is not updated in backend, all this is leading to update the backed as the flags for tracking updation are always true once a modification is made.– JSR29
Nov 22 '18 at 15:22
In the model hook , I must be getting updated as in the Inspect the API call response shows the new data is received, I don't know to print the model details in model hook.
– JSR29
Nov 22 '18 at 15:24
controllers are singletons so their state is not reset during your application lifecycle. If you want this use a component or clean up yourself in
setupController
.– Lux
Nov 22 '18 at 18:09
How using a component would help? Also it is okay to set the variables of controller to set, but my biggest concern is that why my model attributes are not refreshing.
– JSR29
Nov 23 '18 at 7:58
How are you initializing
isChanged
andisCountChanged
? You claim these "are the part of the model's data". Are you storing these values in the database? If you set adebugger
in thesetupController
, is your model data as you expect it? What ismodel.tunein
?– mistahenry
Nov 23 '18 at 10:05