1 (function () { 2 var id = 0; 3 4 var Binder = function (modelObj, modelKey, targetObj, targetAttribute, opts) { 5 opts = opts || {}; 6 this.id = id; 7 id++; 8 this.targetObj = targetObj; 9 this.modelObj = modelObj; 10 this.modelKey = modelKey; 11 this.targetAttribute = targetAttribute; 12 this.opts = opts; 13 this.eventKey = this.modelKey + "_changed"; 14 this.handleChange = _(this.handleChange).bind(this); 15 return this; 16 }; 17 18 //handler receives (payload, targetObj, targetAttribute, model, key); 19 Binder.prototype.handleChange = function (payload) { 20 var data = this.modelObj.get(this.modelKey); 21 if (this.opts.preProcess) { 22 data = this.opts.preProcess(data); 23 } 24 if (this.opts.handler) { 25 this.opts.handler(data, this.targetObj, this.targetAttribute, this.modelObj, this.modelKey); 26 } else { 27 this.targetObj[this.targetAttribute] = data; 28 } 29 }; 30 31 Binder.prototype.listen = function () { 32 this.modelObj.on(this.eventKey, this.handleChange); 33 return this; 34 }; 35 36 Binder.prototype.stopUpdating = function () { 37 this.modelObj.removeListener(this.eventKey, this.handleChange); 38 }; 39 40 41 42 var updaterFunc = function (key, opts) { 43 var binder = new Binder(this, key, opts.object, opts.attribute, opts); 44 binder.listen(); 45 return binder; 46 }; 47 48 MBX.JsModel.extend({ 49 updatesOn: updaterFunc 50 }); 51 52 MBX.JsModel.extendInstancePrototype({ 53 updatesOn: updaterFunc 54 }) 55 56 })(); 57