Skip to content

Commit

Permalink
server-side rendering refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
astoilkov committed Apr 9, 2015
1 parent 45f366b commit 44a346f
Show file tree
Hide file tree
Showing 23 changed files with 127 additions and 83 deletions.
5 changes: 5 additions & 0 deletions .gitattributes
@@ -0,0 +1,5 @@
# Auto detect text files and perform LF normalization
* text=auto

# JS files must always use LF for tools to work
*.js eol=lf
2 changes: 1 addition & 1 deletion bower.json
@@ -1,6 +1,6 @@
{
"name": "blocks",
"version": "0.1.6",
"version": "0.1.7",
"main": "dist/blocks.js",
"license": "MIT",
"author": {
Expand Down
15 changes: 10 additions & 5 deletions dist/blocks-source.js
Expand Up @@ -37,7 +37,7 @@
return value;
};

blocks.version = '0.1.6';
blocks.version = '0.1.7';
blocks.core = core;

/**
Expand Down Expand Up @@ -6107,7 +6107,9 @@ return result;
passRawValues: true,

preprocess: function (domQuery, template, value) {
var serverData = domQuery._serverData;
var html;

template = blocks.$unwrap(template);
if (blocks.isElement(template)) {
html = template.innerHTML;
Expand All @@ -6123,9 +6125,9 @@ return result;
if (value) {
blocks.queries['with'].preprocess.call(this, domQuery, value, '$template');
}
if (!domQuery._serverData) {
if (!serverData || !serverData.templates || !serverData.templates[ElementsData.id(this)]) {
this.html(html);
if (!this._each) {
if (!this._each && this._el != HtmlElement.Empty()) {
this._children = createVirtual(this._el._element.childNodes[0], this);
this._innerHTML = null;
}
Expand Down Expand Up @@ -10926,8 +10928,8 @@ return result;
this._views = {};
this._currentRoutedView = undefined;
this._started = false;
this._serverData = window.__blocksServerData__;
this.options = blocks.extend({}, this.options, options);
this._serverData = null;

this._setDefaults();

Expand Down Expand Up @@ -11182,6 +11184,7 @@ return result;
},

extend: function (obj) {
blocks.extend(this, obj);
clonePrototype(obj, this);
return this;
},
Expand Down Expand Up @@ -11231,6 +11234,7 @@ return result;
},

_ready: function (element) {
this._serverData = window.__blocksServerData__;
this._history = new History(this.options);
this._history
.on('urlChange', blocks.bind(this._urlChange, this))
Expand All @@ -11247,7 +11251,8 @@ return result;
blocks.each(routes, function (route) {
blocks.each(_this._views, function (view) {
if (view.options.routeName == route.id) {
if (!currentView && (view.options.initialPreload || (data.initial && this._serverData))) {
if (!currentView && (view.options.initialPreload ||
(data.initial && _this._serverData && _this.options.history == 'pushState'))) {
view.options.url = undefined;
}
if (currentView && currentView != view) {
Expand Down
15 changes: 10 additions & 5 deletions dist/blocks.js
Expand Up @@ -37,7 +37,7 @@
return value;
};

blocks.version = '0.1.6';
blocks.version = '0.1.7';
blocks.core = core;

/**
Expand Down Expand Up @@ -9097,7 +9097,9 @@ return result;
passRawValues: true,

preprocess: function (domQuery, template, value) {
var serverData = domQuery._serverData;
var html;

template = blocks.$unwrap(template);
if (blocks.isElement(template)) {
html = template.innerHTML;
Expand All @@ -9113,9 +9115,9 @@ return result;
if (value) {
blocks.queries['with'].preprocess.call(this, domQuery, value, '$template');
}
if (!domQuery._serverData) {
if (!serverData || !serverData.templates || !serverData.templates[ElementsData.id(this)]) {
this.html(html);
if (!this._each) {
if (!this._each && this._el != HtmlElement.Empty()) {
this._children = createVirtual(this._el._element.childNodes[0], this);
this._innerHTML = null;
}
Expand Down Expand Up @@ -13923,8 +13925,8 @@ return result;
this._views = {};
this._currentRoutedView = undefined;
this._started = false;
this._serverData = window.__blocksServerData__;
this.options = blocks.extend({}, this.options, options);
this._serverData = null;

this._setDefaults();

Expand Down Expand Up @@ -14179,6 +14181,7 @@ return result;
},

extend: function (obj) {
blocks.extend(this, obj);
clonePrototype(obj, this);
return this;
},
Expand Down Expand Up @@ -14228,6 +14231,7 @@ return result;
},

_ready: function (element) {
this._serverData = window.__blocksServerData__;
this._history = new History(this.options);
this._history
.on('urlChange', blocks.bind(this._urlChange, this))
Expand All @@ -14244,7 +14248,8 @@ return result;
blocks.each(routes, function (route) {
blocks.each(_this._views, function (view) {
if (view.options.routeName == route.id) {
if (!currentView && (view.options.initialPreload || (data.initial && this._serverData))) {
if (!currentView && (view.options.initialPreload ||
(data.initial && _this._serverData && _this.options.history == 'pushState'))) {
view.options.url = undefined;
}
if (currentView && currentView != view) {
Expand Down
8 changes: 4 additions & 4 deletions dist/blocks.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/blocks.min.js.map

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions dist/mvc/blocks-mvc.js
Expand Up @@ -37,7 +37,7 @@
return value;
};

blocks.version = '0.1.6';
blocks.version = '0.1.7';
blocks.core = core;

/**
Expand Down Expand Up @@ -3734,7 +3734,9 @@
passRawValues: true,

preprocess: function (domQuery, template, value) {
var serverData = domQuery._serverData;
var html;

template = blocks.$unwrap(template);
if (blocks.isElement(template)) {
html = template.innerHTML;
Expand All @@ -3750,9 +3752,9 @@
if (value) {
blocks.queries['with'].preprocess.call(this, domQuery, value, '$template');
}
if (!domQuery._serverData) {
if (!serverData || !serverData.templates || !serverData.templates[ElementsData.id(this)]) {
this.html(html);
if (!this._each) {
if (!this._each && this._el != HtmlElement.Empty()) {
this._children = createVirtual(this._el._element.childNodes[0], this);
this._innerHTML = null;
}
Expand Down Expand Up @@ -8564,8 +8566,8 @@
this._views = {};
this._currentRoutedView = undefined;
this._started = false;
this._serverData = window.__blocksServerData__;
this.options = blocks.extend({}, this.options, options);
this._serverData = null;

this._setDefaults();

Expand Down Expand Up @@ -8820,6 +8822,7 @@
},

extend: function (obj) {
blocks.extend(this, obj);
clonePrototype(obj, this);
return this;
},
Expand Down Expand Up @@ -8869,6 +8872,7 @@
},

_ready: function (element) {
this._serverData = window.__blocksServerData__;
this._history = new History(this.options);
this._history
.on('urlChange', blocks.bind(this._urlChange, this))
Expand All @@ -8885,7 +8889,8 @@
blocks.each(routes, function (route) {
blocks.each(_this._views, function (view) {
if (view.options.routeName == route.id) {
if (!currentView && (view.options.initialPreload || (data.initial && this._serverData))) {
if (!currentView && (view.options.initialPreload ||
(data.initial && _this._serverData && _this.options.history == 'pushState'))) {
view.options.url = undefined;
}
if (currentView && currentView != view) {
Expand Down
6 changes: 3 additions & 3 deletions dist/mvc/blocks-mvc.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mvc/blocks-mvc.min.js.map

Large diffs are not rendered by default.

54 changes: 32 additions & 22 deletions dist/node/blocks-node.js
Expand Up @@ -37,7 +37,7 @@
return value;
};

blocks.version = '0.1.6';
blocks.version = '0.1.7';
blocks.core = core;

/**
Expand Down Expand Up @@ -6112,7 +6112,9 @@ return result;
passRawValues: true,

preprocess: function (domQuery, template, value) {
var serverData = domQuery._serverData;
var html;

template = blocks.$unwrap(template);
if (blocks.isElement(template)) {
html = template.innerHTML;
Expand All @@ -6128,9 +6130,9 @@ return result;
if (value) {
blocks.queries['with'].preprocess.call(this, domQuery, value, '$template');
}
if (!domQuery._serverData) {
if (!serverData || !serverData.templates || !serverData.templates[ElementsData.id(this)]) {
this.html(html);
if (!this._each) {
if (!this._each && this._el != HtmlElement.Empty()) {
this._children = createVirtual(this._el._element.childNodes[0], this);
this._innerHTML = null;
}
Expand Down Expand Up @@ -10931,8 +10933,8 @@ return result;
this._views = {};
this._currentRoutedView = undefined;
this._started = false;
this._serverData = window.__blocksServerData__;
this.options = blocks.extend({}, this.options, options);
this._serverData = null;

this._setDefaults();

Expand Down Expand Up @@ -11187,6 +11189,7 @@ return result;
},

extend: function (obj) {
blocks.extend(this, obj);
clonePrototype(obj, this);
return this;
},
Expand Down Expand Up @@ -11236,6 +11239,7 @@ return result;
},

_ready: function (element) {
this._serverData = window.__blocksServerData__;
this._history = new History(this.options);
this._history
.on('urlChange', blocks.bind(this._urlChange, this))
Expand All @@ -11252,7 +11256,8 @@ return result;
blocks.each(routes, function (route) {
blocks.each(_this._views, function (view) {
if (view.options.routeName == route.id) {
if (!currentView && (view.options.initialPreload || (data.initial && this._serverData))) {
if (!currentView && (view.options.initialPreload ||
(data.initial && _this._serverData && _this.options.history == 'pushState'))) {
view.options.url = undefined;
}
if (currentView && currentView != view) {
Expand Down Expand Up @@ -11609,16 +11614,15 @@ return result;

var hasRoute = false;
var hasActive = false;
blocks.each(env.server.applications, function (application) {
application.start();
blocks.each(application._views, function (view) {
if (blocks.has(view.options, 'route')) {
hasRoute = true;
}
if (view.isActive()) {
hasActive = true;
}
});
var application = server.application;
application.start();
blocks.each(application._views, function (view) {
if (blocks.has(view.options, 'route')) {
hasRoute = true;
}
if (view.isActive()) {
hasActive = true;
}
});

if (hasRoute && !hasActive) {
Expand Down Expand Up @@ -11962,19 +11966,19 @@ return result;
var path = require('path');
var express = require('express');

function ServerApplication(options) {
this._options = blocks.extend({}, ServerApplication.Defaults, options);
function Server(options) {
this._options = blocks.extend({}, Server.Defaults, options);
this._app = express();
this._middleware = new Middleware(options);

this._init();
}

ServerApplication.Defaults = blocks.extend({}, Middleware.Defaults, {
Server.Defaults = blocks.extend({}, Middleware.Defaults, {
port: 8000
});

ServerApplication.prototype = {
Server.prototype = {
express: function () {
return this._app;
},
Expand All @@ -11998,7 +12002,11 @@ return result;


blocks.server = function (options) {
return new ServerApplication(options);
return new Server(options);
};

blocks.static = function (options) {

};


Expand Down Expand Up @@ -12067,14 +12075,14 @@ return result;
};

Application.prototype._prepare = function () {
server.applications.push(this);
server.application = this;
};

var viewQuery = blocks.queries.view.preprocess;

blocks.queries.view.preprocess = function (domQuery, view) {
viewQuery.call(this, domQuery, view);
if (view._html) {
if (view._html && server.application && server.application.options.history == 'pushState') {
this._children = parseToVirtual(view._html);
}
};
Expand All @@ -12089,6 +12097,8 @@ return result;
this._children = parseToVirtual(this.html());
this._innerHTML = null;
}
server.data.templates = server.data.templates || {};
server.data.templates[ElementsData.id(this)] = true;
}
};

Expand Down

0 comments on commit 44a346f

Please sign in to comment.