Greg Hinch![]()
When working with complex UI driven applications, sometimes it is useful to have an OO method of dynamically interacting with your data. The Model class is an attempt to bring some useful principles of object relational mapping (at least as I understand them) into YUI. While not necessarily useful unto itself, the Model class forms a good foundation for performing multiple interactions on a single piece of data.
A model is defined as a constructor which extend Y.Model. The ATTRS map is used to define the members of the model, and setters can be used to ensure typing.
In order to use models easily with a DataSource, the DataSourceModelMapper plugin can be used to easily convert the results array into an array of models. The config for the plugin takes a model constructor and a map, an object in which the keys correspond with members in the model, and the values correspond with paths to the data in a result. Much like the resultLocator in a DataSchema, values are strings and can use dot notation to go multiple levels deep to find data.
YUI().use('gallery-model', 'datasource-io', 'datasource-jsonschema', function(Y) {
var Book = Y.Base.create('book', Y.Model, [], {}, {
ATTRS : {
title : {}
author : {},
datePublished : {
setter : "date"
}
});
var bookSource = new Y.DataSource.IO({
source : '/path/to/source.php?'
plugins : [
{fn : Y.Plugin.DataSourceJSONSchema, cfg : {
schema : {
resultFields : ['title', 'author', 'published']
}
}},
{fn : Y.Plugin.DataSourceModelMapper, cfg : {
model : Book,
map : {
author : 'author',
title : 'title',
datePublished : 'published'
}
}}
]
});
bookSource.sendRequest({
request : 'foo=bar',
callback : {
success : function (e) {
var models = e.response.results // will be model instances rather than objects
}
}
});
});No forum posts for this module.
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info