Class with built-in dirty flag.

Uses Proxies to have the dirty flag monitor multiple array inside of the class.

by Bryan Braun

JavaScript

class CommerceAsset {
	constructor(map) {
  	this.dirty = false;
    
    this.changeHandler = {
      set: (obj, prop, value) => {
        obj[prop] = value;
        this.dirty = true;
        return true;
      },
    };
    
    const assetMap = {
    	carts: new Proxy([], this.changeHandler),
      lists: new Proxy([], this.changeHandler)
    };
    
    this.maps = assetMap;
  }

	setNewCart(item) {
	  this.maps.carts.push(item);
    console.log(this.maps);
  }
  
  setNewList(item) {
	  this.maps.lists.push(item);
    console.log(this.maps);
  }
  
  isDirty() {
  	return this.dirty
	}
}

const ca = new CommerceAsset();

console.log(ca.isDirty());

ca.setNewList('hello');

console.log(ca.isDirty());

ca.setNewList('world');

console.log(ca.isDirty());