Navigation Menu

Skip to content

Commit

Permalink
generalizing the View
Browse files Browse the repository at this point in the history
  • Loading branch information
kassens committed Mar 15, 2009
1 parent b0b5380 commit 2a67aee
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Source/Controller.js
@@ -1,3 +1,3 @@
var Controller = new Class({

});
});
73 changes: 55 additions & 18 deletions Source/View.js
@@ -1,9 +1,53 @@
Array.implement({ // TODO: remove for MooTools 1.3
invoke: function(fn, args, bind){
for (var i = 0; i < this.length; i++) this[i][fn].apply(bind, args);
return this;
}
});

var View = new Class({

Implements: Events,

initialize: function(){
this.getters = {};
this.setters = {};
},

toElement: function(){
return this.element;
},

addOutlet: function(name, setter, getter){
this.addSetter(name, setter);
this.setGetter(name, getter);
return this;
},

addSetter: function(name, fn){
if (!this.setters[name]) this.setters[name] = [];
this.setters[name].push(fn);
return this;
},

setGetter: function(name, fn, noOverride){
if (!this.getters[name] || !noOverride) this.getters[name] = fn;
return this;
},

set: function(key, value){
if (typeof key == 'object'){
for (prop in key) this.set(prop, key[prop]);
return this;
}
this.setters[key].each(function(setter){
setter(value);
}, this);
return this;
},

get: function(key){
return this.getters[key]();
}

});
Expand All @@ -14,6 +58,8 @@ View.create = function(specification){
Extends: View,

initialize: function(data){
this.parent();

var element = new Element('div', {html: specification.html}).getFirst();
this.element = element;

Expand All @@ -25,8 +71,15 @@ View.create = function(specification){
};
};

if (specification.outlets) this.outlets = Hash.map(specification.outlets, function(mappings){
return $splat(mappings).map(parseMapping);
if (specification.outlets) Hash.each(specification.outlets, function(mappings, name){
$splat(mappings).map(function(mapping){
mapping = parseMapping(mapping);
this.addSetter(name, function(value){
mapping.element.set(mapping.attribute, value);
}).setGetter(name, function(){
return mapping.element.get(mapping.attribute);
}, true);
}, this);
}, this);

if (specification.actions) Hash.each(specification.actions, function(mappings, action){
Expand All @@ -39,22 +92,6 @@ View.create = function(specification){
}, this);

if (data) this.set(data);
},

set: function(key, value){
if (typeof key == 'object'){
for (prop in key) this.set(prop, key[prop]);
return this;
}
this.outlets[key].each(function(outlet){
outlet.element.set(outlet.attribute, value);
}, this);
return this;
},

get: function(key){
var outlet = this.outlets[key][0];
return outlet.element.get(outlet.attribute);
}

});
Expand Down

0 comments on commit 2a67aee

Please sign in to comment.