Skip to content

Commit

Permalink
Cleaned up environment sendOrRender method and extends Object.merge
Browse files Browse the repository at this point in the history
  • Loading branch information
jimib committed Jan 30, 2013
1 parent 28061f3 commit 0463f05
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
3 changes: 2 additions & 1 deletion app/preinit/environment.js
Expand Up @@ -18,7 +18,8 @@ module.exports = function(app){
res.sendOrRender = function(data, view){
if(view && requestAcceptsHtml()){
//send the data as html
res.locals.data = data;
res.locals = res.locals || {};
Object.merge(res.locals, data);
res.render(view);
}else{
//send the data as json
Expand Down
35 changes: 14 additions & 21 deletions app/preinit/extends.js
Expand Up @@ -4,8 +4,8 @@ module.exports = function(app){
return this.toString().replace(regexp, value);
}

Array.prototype.forEachAsync = function(action, cb){
var self = this;
Array.forEachAsync = function(arr, action, cb){
var self = arr;
var i = - 1;
var lim = this.length;

Expand All @@ -14,7 +14,7 @@ module.exports = function(app){
function next(){
if(++i < lim){
action(self[i], function(err, result){
self[i] = result || self[i];
//self[i] = result || self[i];//? - WHAT IS THIS LINE DOING - NO IDEA COMMENTED OUT
next();
});
}else{
Expand All @@ -23,23 +23,16 @@ module.exports = function(app){
}
}


//ADD A MERGE PROPERTY TO OBJECT
Object.defineProperty(Object.prototype, "merge", {
enumerable: false,
value: function(from, boolOverride) {
if(boolOverride == undefined){
boolOverride = false;
Object.merge = function(a, b, boolOverride){
if(boolOverride == undefined){
boolOverride = false;
}

for(var i in b){
if(boolOverride || a[i] == undefined)
{
a[i] = b[i];
}

var props = Object.getOwnPropertyNames(from);
var dest = this;
props.forEach(function(name) {
if (boolOverride || !(name in dest)) {
dest[name] = from[name];
}
});
return this;
}
});
}
}
}

0 comments on commit 0463f05

Please sign in to comment.