basic fields, set, event.component

This commit is contained in:
David Greenspan
2013-07-25 07:57:26 -07:00
parent 265e58495e
commit 98f43889c4
2 changed files with 29 additions and 3 deletions

View File

@@ -72,6 +72,14 @@ _extend(UI.Component, {
lookup: function (id) {
return this.get(id, true);
},
set: function (id, value) {
var comp = findComponentWithProp(id, this);
if (! comp || ! comp[id])
throw new Error("Can't find field: " + id);
if (! comp[id].$set)
throw new Error("Not a settable field: " + id);
comp[id].$set(value);
},
// convenient syntax
withData: function (data) {
return this.extend({data: data});

View File

@@ -27,7 +27,24 @@ UI.makeTemplate = function (underlying) {
var oldProp = underlying[k];
var givenProp = options[k];
if (k.indexOf(' ') >= 0) {
if (k === 'fields') {
// XXX basic fields impl
// XXX could support functions as initial field values
_.each(givenProp, function (fieldValue, fieldName) {
var getter = function () {
getter.$dep.depend();
return getter.$value;
};
getter.$value = fieldValue; // initial value
getter.$dep = new Deps.Dependency;
getter.$set = function (v) {
getter.$value = v;
getter.$dep.changed();
};
underlying[fieldName] = getter;
});
} else if (k.indexOf(' ') >= 0) {
// event handler
// XXX clean up
var eventType = k.slice(0, k.indexOf(' '));
@@ -40,8 +57,9 @@ UI.makeTemplate = function (underlying) {
handler: (function (handler) {
return function (evt) {
// XXX
var data = UI.body.findByElement(
evt.currentTarget).get();
var comp = UI.body.findByElement(evt.target);
evt.component = comp;
var data = comp.get();
handler.call(data, evt);
};
})(givenProp)