Files
meteor/packages/ui/forms.js
David Greenspan 2eac570757 augment->include, no more "implicit augment"
It's just too useful for Component constructors to be
callable as functions:

Each.create({content: Template.foo})

// in impl of Each's render:
buf(this.content(function () { return 3; }))

With implicit augment, `this.content(…)` would augment Template.foo.

Also, there was the ever-present danger of someone omitting `new`,
which we couldn't detect.

Now `new Foo(…)`, `Foo(…)`, and `Foo.create(…)` are all equivalent.
2013-07-09 18:01:50 -07:00

49 lines
1.2 KiB
JavaScript

var Component = UIComponent;
var getterImpl =
function (foo) {
var fooDep = foo + "Dep";
var _foo = "_" + foo;
return function () {
this[fooDep].depend();
return this[_foo];
};
};
Component.include({
extendHooks: {
fields: function (dict) {
var proto = this.prototype;
var type = this;
for (var fieldName in dict)
proto[fieldName] = getterImpl(fieldName);
type.include({
constructed: function () {
for (var fieldName in dict) {
this["_" + fieldName] = dict[fieldName];
this[fieldName + "Dep"] = new Deps.Dependency;
}
}
});
}
},
set: function (fieldName, fieldValue) {
var _foo = "_" + fieldName;
var fooDep = fieldName + "Dep";
if ((_foo in this) && (fooDep in this)) {
// XXX compare with something besides `===`?
// do fields have to be EJSON or can they be anything?
if (fieldValue !== this[_foo]) {
this[_foo] = fieldValue;
if (fooDep in this)
this[fooDep].changed();
}
} else {
throw new Error("No such field: " + fieldName);
}
}
});