|
@@ -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;
|
|
|
}));
|