trip.core
Version:
53 lines (41 loc) • 1.89 kB
JavaScript
;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var ee = require('event-emitter');
var View = function () {
function View(model, scene) {
var _this2 = this;
_classCallCheck(this, View);
this.model = model;
this.scene = scene;
ee(this);
// Respond to model 'change' events by triggering an 'update'
// event
var _this = this;
// 'arguments' doesn't work with arrow function here, so use classic function()
this.changeFn = function () {
_this.emit('pre_update');
_this.update.apply(_this, arguments);
_this.emit('post_update');
};
// Render on next tick to ensure view
// construction is complete
setTimeout(function () {
_this.emit('pre_render');
_this.render();
_this.emit('post_render');
// It can happen that a change event is fired before the
// next tick, so only start responding to changes after
// we are satisfied render() has been called
model.on('change', _this2.changeFn);
}, 0);
}
_createClass(View, [{
key: 'remove',
value: function remove() {
this.model.off('change', this.changeFn);
}
}]);
return View;
}();
module.exports = View;