updchg.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // UMD as of 14 May 2015 from github/umdjs/umd.
  2. (function (root, factory) {
  3. if (typeof define === 'function' && define.amd) {
  4. // AMD. Register as an anonymous module.
  5. define([], factory);
  6. } else if (typeof exports === 'object') {
  7. // Node. Does not work with strict CommonJS, but
  8. // only CommonJS-like environments that support module.exports,
  9. // like Node.
  10. module.exports = factory();
  11. } else {
  12. // Browser globals (root is window)
  13. root.returnExports = factory();
  14. }
  15. }(this, function () {
  16. function mixin(src, dst) {
  17. Object.keys(src).forEach(function (k) {
  18. if (typeof src[k] === "undefined") delete dst[k];
  19. else dst[k] = src[k];
  20. });
  21. }
  22. // Model-like entity (one that sends update event).
  23. // Uses jQuery-like (jQuery, zepto, ...) event mechanism.
  24. // Must set JQUpd.jQuery to the wrapper function
  25. // before actual use.
  26. function JQUpd() {
  27. }
  28. JQUpd.prototype.update = function (data) {
  29. mixin(data, this);
  30. JQUpd.jQuery(this).trigger("changed", data);
  31. };
  32. // View-like entity (one that observes changed event).
  33. // Uses jQuery-like (jQuery, zepto, ...) event mechanism.
  34. function JQChg() {
  35. }
  36. JQChg.prototype.observeModel = function (model) {
  37. var self = this;
  38. model.on("changed", function (ev, data) {
  39. self.modelChanged(data, this);
  40. })
  41. };
  42. return {
  43. JQUpd: JQUpd,
  44. JQChg: JQChg
  45. };
  46. }));