still working to simplify the action architecture...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2014-09-04 06:45:04 +04:00
parent 9ed14ba3b8
commit b9e25f0c03

View File

@ -17,20 +17,36 @@ data = require('data')
/*********************************************************************/
// attr can be:
// "name" - attribute name
// "name, name, ..."
// - string containign coma separated attribute names
// list - list of attribute names
//
// XXX add a callback here...
function proxy(attr, name){
// we can proxy multiple attrs...
attr = typeof(attr) == typeof('str')
? attr.split(',').map(function(e){ return e.trim() })
: attr
return function(){
attr = this[attr]
attr[name].apply(attr, arguments)
var that = this
var args = arguments
attr.forEach(function(a){
a = that[a]
a[name].apply(a, args)
})
return this
}
}
function proxyMethods(obj, map){
var txt = ''
map = map == null ? obj : map
for(var attr in map){
var methods = map[attr]
methods = typeof(methods) == typeof('str') ? {attr: methods} : methods
for(var name in methods){
var txt = methods[name]
if(txt == null){
@ -45,6 +61,7 @@ function proxyMethods(obj, map){
/*********************************************************************/
// This will:
@ -63,45 +80,59 @@ var ClientPrototype = {
// .data
//
// direct proxy methods...
focusImage: 'Focus image',
focusRibbon: 'Focus ribbon',
firstImage: 'Focus first image in current ribbon',
lastImage: 'Focus last image in current ribbon',
// XXX client-specific API...
// XXX
}
// setup the proxy methods...
var ClientPrototype = proxyMethods(
ClientPrototype,
{
data: {
focusImage: 'Focus image',
focusRibbon: 'Focus ribbon',
firstImage: 'Focus first image in current ribbon',
lastImage: 'Focus last image in current ribbon',
},
})
// XXX this is temporary...
// ...this will messup actual methods...
proxyMethods(ClientPrototype)
/*********************************************************************/
var Client =
module.Client =
function Client(){
// in case this is called as a function (without new)...
if(this.constructor.name != 'Client'){
return new Client()
}
// XXX setup initial state...
return this
// XXX auto apply this...
function chainSelfAttrMethods(cls, attr, meth){
return function(){
// NOTE: this is super, python-style but without multiple
// inheritance...
// ...that last part makes this more of a code reuse
// than a programming tool...
cls.__proto__[meth].apply(this, arguments)
// call the encapsulated method...
this[attr][meth].apply(this[attr], arguments)
return this
}),
}
Client.__proto__ = ClientClassPrototype
Client.prototype = ClientPrototype
Client.prototype.constructor = Client
var ViewerPrototype = {
// this expects the folowing attrs:
//
// .ribbons
//
focusImage: doc('Focus image',
chainSelfAttrMethods(ViewerPrototype, 'ribbons', 'focusImage')),
focusRibbon: doc('Focus ribbon',
chainSelfAttrMethods(ViewerPrototype, 'ribbons', 'focusRibbon')),
}
ViewerPrototype.__proto__ = ClientPrototype
var Client =
module.Client = Object.create(ClientPrototype)
var Viewer =
module.Viewer = Object.create(ViewerPrototype)
/**********************************************************************