Permalink
Browse files

comments for bind

  • Loading branch information...
1 parent 48f6c3b commit 400bdb3ec10031d9301130fb723855c04e76c3eb @rhysbrettbowen committed Feb 27, 2012
Showing with 17 additions and 5 deletions.
  1. +16 −4 model.js
  2. +1 −1 testApp/testApp.js
View
@@ -222,8 +222,20 @@ mvc.Model.prototype.save = function() {
this.sync_.update(this);
};
-// binds a change event
+mvc.Model.prototype.getBinder = function(key) {
+ return goog.bind(this.set, this, key);
+}
+
/**
+ * Allows easy binding of a model's attributre to an element or a function.
+ * bind('name', Element(s)) would change the text content to the model's name
+ * bind('name', Element(s), function(El, attr)) runs a function with the element
+ * and the attributes value
+ * bind('name', function(value), handler) allows you to run a function and
+ * optionally bind it to the handler
+ * if no name is passed (null or undefined) then the operation will be run on
+ * any change to the model and pass in the model
+ *
* @param {string} name
* @param {Element|Node|Function|*} el
* @param {Function|*} fn
@@ -240,11 +252,11 @@ mvc.Model.prototype.bind = function(name, el, fn) {
el = [el];
goog.array.forEach(el, function(elem) {
if(goog.isFunction(fn)) {
- fn(elem, changes[name]);
+ fn(elem, changes[name] || this);
} else if (el.nodeType){
- goog.dom.setTextContent(elem, changes[name]);
+ goog.dom.setTextContent(elem, changes[name] || this);
} else {
- el = changes[name];
+ el = changes[name] || this;
}
});
}
View
@@ -5,7 +5,7 @@ document.write('<div id="note"><div><input type="text" id="idinput" class="idinp
var sync = new mvc.sync.Local();
// text must have newlines beginning with a hash
-var schema = new mvc.model.Schema({'text': /\n#/});
+var schema = new mvc.model.Schema({'text': /copyright/});
// create a new model and push to localStorage
var model = new mvc.Model({'text': 'default'}, schema, sync);

0 comments on commit 400bdb3

Please sign in to comment.