mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
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.
49 lines
1.2 KiB
JavaScript
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);
|
|
}
|
|
}
|
|
}); |