Browse Source

much more flexible and lightweight; release as 0.2.0

Herbert Vojčík 9 years ago
parent
commit
c531bb6786
2 changed files with 34 additions and 23 deletions
  1. 1 1
      bower.json
  2. 33 22
      lib/updchg.js

+ 1 - 1
bower.json

@@ -1,6 +1,6 @@
 {
   "name": "updchg",
-  "version": "0.1.0",
+  "version": "0.2.0",
   "homepage": "https://github.com/herby/updchg",
   "authors": [
     "Herbert Vojčík <herby@mailbox.sk>"

+ 33 - 22
lib/updchg.js

@@ -21,32 +21,43 @@
         });
     }
 
-    // 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() {
+    function UpdChg(event, methodName) {
+        // event name used; defaults to "changed"
+        this.event = event || "changed";
+
+        // method name to pass changes into; defaults to "modelChanged"
+        this.methodName = methodName || "modelChanged";
     }
 
-    JQUpd.prototype.update = function (data) {
-        mixin(data, this);
-        JQUpd.jQuery(this).trigger("changed", data);
-    };
+    // For jQuery-like events,
+    // call this with appropriate wrapper function
+    // (jQuery, zepto, ...) to get an instance
+    // with proper 'update' and 'observe' to mix in:
+    // var updChg = new UpdChg(...).jQuery(jQuery);
+    UpdChg.prototype.jQuery = function ($) {
+        var event = this.event,
+            methodName = this.methodName;
 
-    // View-like entity (one that observes changed event).
-    // Uses jQuery-like (jQuery, zepto, ...) event mechanism.
-    function JQChg() {
-    }
+        return {
+            // update signalling; using jQuery-like events
+            // mix in: `ModelClass.prototype.update = updChg.update;`
+            update: function (data) {
+                mixin(data, this);
+                $(this).trigger(event, data);
+            },
 
-    JQChg.prototype.observeModel = function (model) {
-        var self = this;
-        model.on("changed", function (ev, data) {
-            self.modelChanged(data, this);
-        })
+            // observing; using jQuery-like events
+            // mix in: `ViewClass.prototype.observeModel = updChg.observe;`
+            // and make view instance call it once at the startup,
+            // or, just make observing externally: `updChg.observe.call(view, model);`
+            observe: function (model) {
+                var self = this;
+                $(model).on(event, function (ev, data) {
+                    self[methodName](data, this);
+                });
+            }
+        };
     };
 
-    return {
-        JQUpd: JQUpd,
-        JQChg: JQChg
-    };
+    return UpdChg;
 }));