Skip to content

Instantly share code, notes, and snippets.

@mattparker
Created September 22, 2010 08:29
Show Gist options
  • Save mattparker/591362 to your computer and use it in GitHub Desktop.
Save mattparker/591362 to your computer and use it in GitHub Desktop.
YUI.add('fielddef', function (Y) {
/**
* A field definition sets up everything except the actual value of a field.
*
*/
Y.FieldDef = Y.Base.create(
'fielddef',
Y.Base,
[],
{},
{
ATTRS: {
key: {
},
label: {
},
/**
* Validator function to check value with.
* @attribute validator
*/
validator: {
},
editor: {
},
formatter: {
},
//
dirty: {
}
}
});
},'0.9',{requires:['base']});
YUI.add('field', function (Y) {
/**
* A Field is just a FieldDef with a value
*/
Y.Field = Y.Base.create(
'field',
Y.FieldDef,
[],
{
/**
* This might be a mistake, but makes it a bit quicker/easier to
* get the field value
*/
toString: function(){
return this.get("value");
}
},
{
ATTRS: {
value: {
validator: function() {
var validFn = this.get("validator");
if (Y.Lang.isFunction(validFn)) {
return validFn.apply(this,arguments);
}
return true;
}
}
}
});
},'0.9',{requires:['fielddef', 'base']});
/**
* RecordBase is the base class that Record and RecordSet both extend.
*/
YUI.add('record-base', function(Y) {
Y.RecordBase = Y.Base.create(
'record-base',
Y.Base,
[Y.ArrayList],
{
/**
* Initializer inherited from Y.Base and overridden
* @method initializer
* @param cfg {object} Initial configuration (not used)
*/
initializer: function(cfg) {
this._items = [];
this._keyHash = {};
},
/**
* @description THIS MAY BE A BAD IDEA
* By default works as it should: if there's an attribute set
* to key then this will return it. If not, it will look
* for a field named key and return that.
* Allows shortcut to get field values by key.
*
* @method get
* @public
*/
get: function(key) {
var v = Y.RecordBase.superclass.get.call(this, key);
if (v) {
return v;
} else {
return this._getChild(key);
}
},
/**
* @method _getChild
* @description Gets a child item by key
* @protected
*/
_getChild: function(key) {
return this._items[this._keyHash[key]];
},
/**
* @description Adds a child to the set of items
* @method add
* @public
* @chainable
*/
add: function(v, index) {
if (!(v instanceof Y[this.CHILD_CLASS])) {
v = new Y[this.CHILD_CLASS](v);
}
this._keyHash[v.get("key")] = this.size();
v.set("parent", this);
Y.ArrayList.prototype.add.call(this,v, index);
return this;
}
},
{
CHILD_CLASS: "",
ATTRS: {
key: {
},
/**
* @description The containing instance - for Records, this is probably a RecordSet.
* Added here to allow a RecordSet of RecordSets (maybe)...
*/
parent:{
}
}
});
},'0.9',{requires:['base']});
/**
* A Record holds a set of Fields
*/
YUI.add('record', function (Y) {
Y.Record = Y.Base.create(
'record',
Y.RecordBase,
[],
{
CHILD_CLASS: "Field",
/**
* @method getField
* @description Gets a field by name
* @public
*/
getField: function(key) {
return this._getChild(key);
}
},
{
ATTRS: {
// this might be the primary key to allow some indexing
key: {
},
/**
* @description Set of fields or object literals that will be made into
* fields when they're added
*/
fields: {
setter: function(v) {
Y.Array.each(v, function(i) {
this.add(i);
}, this);
return this._items;
},
lazyAdd: false
}
}
});
},'0.9',{requires:['record-base']});
/**
* A RecordSet holds Record instances
*/
YUI.add('recordset', function (Y) {
Y.RecordSet = Y.Base.create(
'recordset',
Y.RecordBase,
[],
{
CHILD_CLASS: "Record",
/**
* @method getField
* @description Gets a field by name
* @public
*/
getRecord: function(key) {
return this._getChild(key);
}
},
{
ATTRS: {
key: {
},
/**
* @description A set of FieldDef instances that define the fields
* in each Record. Optional. Using this means that every Record will be
* of the same type.
*/
fieldDefs: {
},
/**
* @description An array of records or object literals that will be made
* into Record instances
*/
records: {
setter: function(v) {
Y.Array.each(v, function(i) {
this.add(i);
}, this);
return this._items;
},
lazyAdd: false
}
}
});
},'0.9',{requires:['record-base']});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment