/ * * * Provided listener watch.js * / Export function setWatcher (Page) { the let Data = page.data; the let Watch = page.watch; Object.keys (Watch) .forEach (V => { the let Key = V .split ( '.'); // to watch the property to cut into an array of '.' the let nowData = data; // assign data to nowData for (the let I = 0; I <key.length -. 1; I ++ ) { // iterate key elements of the array, except the last one! nowData = nowData [key [I]]; // to point to its key attribute nowData objects } the let lastKey in = key [key.length -. 1 ]; // assumed key === 'my.name', this time === Data nowData [ 'My'] === data.my, lastKey in === 'name' the let watchFun Watch = [V] .handler || Watch [V]; // compatible and two way tape handler with the handler without the let Deep Watch = [V] .deep; // If not set deep, compared undefine the observe (nowData, lastKey in, watchFun, deep, Page) ; // listening nowData object lastKey in }) } / * * * listener listens properties and perform the function * / function the observe (obj, Key, watchFun, deep, Page) { var Val = obj [Key]; // Analyzing is deep val true and not empty and typeof val === 'object' (the array value changes also need to monitor the depth) IF (val deep &&! = null &&typeof=== val 'Object' ) { Object.keys (val) .forEach (childKey => { // through each key val objects under the observe (val, childKey, watchFun, Deep, Page); // recursive call monitor function }) } the let that = the this ; Object.defineProperty (obj, Key, { Configurable: to true , Enumerable: to true , SET: function (value) { watchFun.call (Page, value, Val); // value is the new value , val old value Val = value; IF (deep) { // If the depth of the monitor, the listener again the object, in order to monitor its properties. observe(obj, key, watchFun, deep, page); } }, get: function () { return val; } }) } module.exports = { setWatcher: setWatcher }
Watch The require = const ( "../../../ utils / watch.js" ); / * * * Life Cycle function - listen for page loads * / onLoad: function (Options) { watch.setWatcher ( the this ) ; // set the listener, it is recommended to call at the onLoad // remove login the this .getLOginInfo (); }, Watch: { 'prescriptionInfo.prescr_type': function (value, oldValue) { IF (value == 0 ) { / / the console.log ( 'prescription medicine'); the let bottled = 'prescriptionInfo.is_bottled' ; the let taste_type = 'prescriptionInfo.taste_type' ; let is_pregnant = 'prescriptionInfo.is_pregnant'; let is_suffering = 'prescriptionInfo.is_suffering'; let suffering_num = 'prescriptionInfo.suffering_num'; this.setData({ [bottled]: 0, [taste_type]: 1, [is_pregnant]: 0, [is_suffering]: 0, [suffering_num]: 1, }); } if (value == 2) { // console.log('膏方'); let is_pregnant = 'prescriptionInfo.is_pregnant'; let is_suffering = 'prescriptionInfo.is_suffering'; let suffering_num = 'prescriptionInfo.suffering_num'; this.setData({ [is_pregnant]: 0, [is_suffering]: 0, [suffering_num]: 3, }); } }, },