question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Computed is not firing

See original GitHub issue

For 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:closed
  • Created 5 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ItamarShDevcommented, Feb 24, 2019

@gs-trader if this matter is solved for you, please consider closing the issue

1reaction
mweststratecommented, Feb 9, 2019

@gs-trader yes, form must be declared as observable, otherwise mobx cannot track reassignments to that property! And you are reassinging it in setFormData: 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 as readonly would have resulted in a compile error in typescript in the setFormData method.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found