Permalink
Browse files

Add proxy and update page to use it

  • Loading branch information...
1 parent 0f3be23 commit 332469f8d0f47f1965d3940f186433d7a7844d37 @kriszyp committed Feb 11, 2014
Showing with 115 additions and 0 deletions.
  1. +89 −0 core/Proxy.js
  2. +1 −0 core/base.js
  3. +25 −0 page.js
View
@@ -0,0 +1,89 @@
+define('xstyle/core/Proxy', ['xstyle/core/utils'], function(utils){
+ var when = utils.when;
+
+ function Proxy(value){
+ // just assume everything will be observed, we could change the optimizations later
+ if(value){
+ this.setSource(value);
+ }
+ }
+ Proxy.prototype = {
+ property: function(key){
+ var properties = this.hasOwnProperty('_properties') ?
+ this._properties : (this._properties = {});
+ var proxy = properties[key];
+ if(proxy){
+ return proxy;
+ }
+ proxy = properties[key] = new Proxy(this.get(key));
+ proxy.parent = this;
+ proxy.name = key;
+ return proxy;
+ },
+ observe: function(listener){
+ (this._listeners || (this._listeners = [])).push(listener);
+ if(this.source){
+ listener(this.valueOf());
+ }
+ // TODO: return handle with remove method
+ },
+ setSource: function(source){
+ var proxy = this;
+ if(this.handle && this.handle.remove){
+ this.handle.remove();
+ }
+ when(source, function(source){
+ proxy.source = source;
+ var listeners = proxy._listeners || 0;
+ var value = proxy.valueOf();
+ for(var i = 0; i < listeners.length; i++){
+ listeners[i](value);
+ }
+ if(source && source.observe){
+ proxy.handle = source.observe(function(value){
+ var listeners = proxy._listeners || 0;
+ for(var i = 0; i < listeners.length; i++){
+ listeners[i](value);
+ }
+ });
+ }
+ var properties = proxy._properties;
+ for(i in properties){
+ proxy.property(i).setSource(source && source.property && source.property(i));
+ }
+ });
+ },
+ valueOf: function(){
+ var source = this.source;
+ return source && source.observe ? source.valueOf() : source;
+ },
+ get: function(key){
+ var source = this.source;
+ return source ?
+ source.get ? source.get(key) : source[key] :
+ this['value-' + key];
+ },
+ set: function(key, value){
+ var source = this.source;
+ if(source){
+ source[key] = value;
+ }else{
+ this['value-' + key] = value;
+ }
+ var property = this._properties && this._properties[key];
+ if(property){
+ property.put(value);
+ }
+ },
+ put: function(value){
+ var source = this.source;
+ if(source && source.put){
+ return source.put(value);
+ }else if(this.parent){
+ this.parent[this.name] = value;
+ }
+ return this.setSource(value);
+ }
+ };
+ return Proxy;
+});
View
@@ -110,6 +110,7 @@ define('xstyle/core/base', [
// TODO: add url()
// adds support for referencing each item in a list of items when rendering arrays
item: elementProperty('item'),
+ 'page-content': elementProperty('page-content'),
// adds referencing to the prior contents of an element
content: elementProperty('content', function(target){
target.appendChild(this.element);
View
25 page.js
@@ -0,0 +1,25 @@
+define(['xstyle/core/base', 'xstyle/core/Proxy', 'xstyle/core/parser', 'xstyle/main', 'dojo/domReady!'], function(base, Proxy, parse){
+ var contentText = document.getElementsByTagName('pre')[0].innerHTML,
+ body = document.body,
+ entities = {
+ '&lt;': '<',
+ '&gt;': '>',
+ '&amp;': '&'
+ };
+
+ body._contentNode = body;
+ body.innerHTML = '';
+ contentText = contentText.replace(/&\w+;/g, function(entity){
+ return entities[entity];
+ });
+
+ var content = eval('(' + contentText + ')');
+ (body['page-content'] || (body['page-content'] = new Proxy())).setSource(content);
+ /*base.newRule('content');
+
+ parse(contentRule, contentText, {
+ rules: [],
+ addRule: function(){}
+ });*/
+
+});

0 comments on commit 332469f

Please sign in to comment.