Computed is not firing
See original GitHub issueFor given code when onFieldChange is called, the computed property named editedFields is not updating. Any help will be appreciated.
class FormsBaseStore {
constructor() {
this.form = {};
}
//Filters the form fields that were edited
@ computed get editedFields() {
let arrEdited = [];
const fields = this.form.fields;
for (var field in fields) {
(fields[field].isEdited)
? arrEdited.push(fields[field])
: null;
}
return arrEdited;
}
//Use it to instantiate values to the form
@ action setFormData(fieldsObj) {
const {
id,
...fields
} = fieldsObj;
let newFieldsObj = {};
for (var field in fields) {
const newObjValue = (fields[field].value !== null && fields[field].value !== undefined) ? fields[field].value : "";
const newObjRule = (fields[field].rule) ? fields[field].rule : null;
(newObjValue !== undefined && newObjValue !== null && newObjValue !== "")
? newFieldsObj[field] = new FormField(field, newObjValue, )
: newFieldsObj[field] = new FormField(field, newObjValue);
}
this.form = new Form(newFieldsObj, id);
}
//this method is called on the onChange event of the form elements
@ action.bound onFieldChange(field, value) {
var fieldName = (typeof(field) === "string") ? field : field.target.name;
const form = this.form;
const formField = form.fields[fieldName];
//Setting item value
(value == undefined || value == null) ? value = "" : null;
let fieldValue = (value !== undefined && value !== "") ? value : (typeof(field) === "string") ? "" : field.target.value;
(fieldValue == undefined || fieldValue == null) ? fieldValue = "" : fieldValue;
formField.value = fieldValue;
//controlling edition
this.checkIfEdited(formField);
}
checkIfEdited = (field) => {
let value = field.value;
let defaultValue = field.defaultValue;
if (isObservable(field.defaultValue)) {
defaultValue = toJS(field.defaultValue);
value = toJS(field.value);
}
if (defaultValue !== value && !Array.isArray(defaultValue) && typeof defaultValue !== 'object') {
field.isEdited = true;
} else if ((Array.isArray(defaultValue) || typeof defaultValue === 'object') && !isEqual(defaultValue, value)) {
field.isEdited = true;
} else if (!field.isValid) {
field.isEdited = true;
} else {
field.isEdited = false;
field.container = null;
}
}
}
class Form {
constructor(fields, id) {
this.fields = fields;
this.meta.id = id;
}
@ observable fields = {};
@ observable meta = {
id: null
}
}
class FormField {
@ observable name;
@ observable value;
@ observable isEdited;
constructor(name = null, value = "", isEdited = false) {
this.name = name;
this.value = value;
this.isEdited = isEdited;
}
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (6 by maintainers)
Top Results From Across the Web
Vue.js computed property not working - Stack Overflow
Another reason: properties with an underscore ( _ ) prefix will not trigger computed . Surprise: _ and $ prefixes are reserved in...
Read more >Vue computed method not firing in certain scenario - Get Help
I have a slight issue with one of my Vue components computed property. It gets called here: <GMap class="c-splitview__map" ...
Read more >Vue Computed Property not updating; Troubleshooting Steps
How to troubleshoot the problems that occurred when the computed property does not work or update what we want is explained in this...
Read more >Vue: When a computed property can be the wrong tool
There is a downside to this: If the result returned by a computed property can only be known after your code makes use...
Read more >ko.computed() is not triggered when observable value ...
In your case, you are changing the state of object inside the array (e.g the amount variable of object inside the array). Since...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@gs-trader if this matter is solved for you, please consider closing the issue
@gs-trader yes,
form
must be declared as observable, otherwise mobx cannot track reassignments to that property! And you are reassinging it insetFormData
:this.form = new Form(newFieldsObj, id);
A rule of thumb you can use: either all fields should be
readonly
or@observable
. If it is neither, than you have something for which no changes can be tracked. Marking that field asreadonly
would have resulted in a compile error in typescript in thesetFormData
method.