Browse Source

jQuery-based update/change

Herbert Vojčík 9 years ago
parent
commit
0de376ec66
1 changed files with 52 additions and 0 deletions
  1. 52 0
      lib/updchg.js

+ 52 - 0
lib/updchg.js

@@ -0,0 +1,52 @@
+// UMD as of 14 May 2015 from github/umdjs/umd.
+(function (root, factory) {
+    if (typeof define === 'function' && define.amd) {
+        // AMD. Register as an anonymous module.
+        define([], factory);
+    } else if (typeof exports === 'object') {
+        // Node. Does not work with strict CommonJS, but
+        // only CommonJS-like environments that support module.exports,
+        // like Node.
+        module.exports = factory();
+    } else {
+        // Browser globals (root is window)
+        root.returnExports = factory();
+    }
+}(this, function () {
+
+    function mixin(src, dst) {
+        Object.keys(src).forEach(function (k) {
+            if (typeof src[k] === "undefined") delete dst[k];
+            else dst[k] = src[k];
+        });
+    }
+
+    // Model-like entity (one that sends update event).
+    // Uses jQuery-like (jQuery, zepto, ...) event mechanism.
+    // Must set JQUpd.jQuery to the wrapper function
+    // before actual use.
+    function JQUpd() {
+    }
+
+    JQUpd.prototype.update = function (data) {
+        mixin(data, this);
+        JQUpd.jQuery(this).trigger("changed", data);
+    };
+
+    // View-like entity (one that observes changed event).
+    // Uses jQuery-like (jQuery, zepto, ...) event mechanism.
+    function JQChg() {
+    }
+
+    JQChg.prototype.observeModel = function (model) {
+        var self = this;
+        model.on("changed", function (ev, data) {
+            self.modelChanged(data, this);
+        })
+    };
+
+    return {
+        JQUpd: JQUpd,
+        JQChg: JQChg
+    };
+}));