Skip to content

Instantly share code, notes, and snippets.

@willbailey
Created February 25, 2011 17:39
Show Gist options
  • Save willbailey/844168 to your computer and use it in GitHub Desktop.
Save willbailey/844168 to your computer and use it in GitHub Desktop.
commit 2111cdc41fbd683e43b33a38a7f4b60670b3013b
Author: wbailey <wi11.bai1ey@facebook.com>
Date: Fri Feb 25 09:50:40 2011 -0800
model change event is now just "changed"
diff --git a/src/binding.js b/src/binding.js
index 668c9a9..dafbee1 100644
--- a/src/binding.js
+++ b/src/binding.js
@@ -22,7 +22,7 @@ JX.install('Binding', {
util.bind(this.updateModel, this));
this.modelToken = this.getModel().listen(
- this.getModelEvent(),
+ 'changed',
util.bind(this.updateView, this));
if (options.sync !== false) {
@@ -70,8 +70,11 @@ JX.install('Binding', {
},
updateView: function(event) {
- if (this.getViewValue() != this.getModelValue() &&
- event.source !== this) {
+ var modelProperty = this.getModelProperty();
+ var modelValue = this.getModelValue();
+ var viewValue = this.getViewValue();
+ var isRelevant = (event.changedProperties || {})[modelProperty];
+ if (isRelevant && viewValue !== modelValue && event.source !== this ) {
this.setViewValue(this.getModelValue());
}
}
diff --git a/src/model.js b/src/model.js
index 7a2300e..51387f1 100644
--- a/src/model.js
+++ b/src/model.js
@@ -14,8 +14,25 @@ JX.install('Model', {
},
set: function(key, value) {
- var setter = util.setter(key);
- return this[setter] && this[setter](value);
+ var prop = {};
+ prop[key] = value;
+ return this.setAll(prop);
+ },
+
+ setAll: function(obj) {
+ var changedProperties = {};
+ util.forEach(obj, function(value, key) {
+ var hidden = '__auto__' + key;
+ var prev = this[hidden];
+ if (!util.isEqual(prev, value)) {
+ this[hidden] = value;
+ changedProperties[key] = prev;
+ }
+ }, this);
+ if (!util.isEmpty(changedProperties)) {
+ this.invoke('changed', {changedProperties: changedProperties});
+ }
+ return this;
}
}
@@ -28,19 +45,13 @@ exports.install = function(name, junk) {
junk.members = junk.members || {};
util.forEach(junk.properties, function(value, key) {
- var hidden = '__auto__' + key;
- var changed = key + 'Changed';
-
- junk.events.push(changed);
-
+ junk.events.push('changed');
junk.members[util.getter(key)] = function() {
- return this[hidden] || value;
+ return this['__auto__' + key] || value;
};
junk.members[util.setter(key)] = function(value) {
- var prev = this[hidden];
- this[hidden] = value;
- this.invoke(changed, {value: prev});
+ this.set(key, value);
return this;
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment