Permalink
Browse files

View change: if no selector is given a mapping defaults to the top le…

…vel element, f.e. "@html"
  • Loading branch information...
1 parent badeb46 commit f80949396634cede2ca9b18c06b6487fa73b87c0 @kassens committed Mar 10, 2009
Showing with 17 additions and 11 deletions.
  1. +17 −11 Source/View.js
View
@@ -9,25 +9,31 @@ View.create = function(specification){
return new Class({
Extends: View,
initialize: function(data){
- this.element = new Element('div', {html: specification.html}).getFirst();
+ var element = new Element('div', {html: specification.html}).getFirst();
+ this.element = element;
+
+ var parseMapping = function(mapping){
+ mapping = mapping.split('@');
+ return {
+ element: mapping[0] ? element.getElement(mapping[0]) : element,
+ attribute: mapping[1]
+ };
+ };
+
if (specification.outlets) this.outlets = Hash.map(specification.outlets, function(mappings){
- return $splat(mappings).map(function(mapping){
- mapping = mapping.split('@');
- return {
- element: this.element.getElement(mapping[0]),
- attribute: mapping[1]
- };
- }, this);
+ return $splat(mappings).map(parseMapping);
}, this);
+
if (specification.actions) Hash.each(specification.actions, function(mappings, action){
$splat(mappings).map(function(mapping){
- mapping = mapping.split('@');
- this.element.getElement(mapping[0]).addEvent(mapping[1], (function(event){
+ mapping = parseMapping(mapping);
+ mapping.element.addEvent(mapping.attribute, (function(event){
this.fireEvent(action, event);
}).bind(this));
}, this);
}, this);
- this.set(data);
+
+ if (data) this.set(data);
},
set: function(key, value){
if (typeof key == 'object'){

0 comments on commit f809493

Please sign in to comment.