Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 41 additions & 40 deletions server/miso.util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Various utilities that normalise usage between client and server
// This is the server version
// See /client/miso.util.js for client version
var _ = require("lodash");

module.exports = {
// Are we on the server?
Expand Down Expand Up @@ -73,44 +74,44 @@ module.exports = {
obj.query[key]: def;
},

// Testing ready binder
// NOTE: We somehow need to share this with the createRoute method...
readyBinderFactory: function(){
var bindings = [],
afterBindings = [],
myBindings = {
bind: function(cb) {
bindings.push(cb);
// Testing ready binder
// NOTE: We somehow need to share this with the createRoute method...
readyBinderFactory: function(){
var bindings = {},
afterBindings = [],
myBindings = {
bind: function(cb) {
bindings[_.size(bindings)] = cb;

// Return a function that will remove this binding and
// fire the ready function if there are no more bindings
return (function(index){
return function(){
bindings[index]();
bindings.splice(index, 1);
if(!myBindings.hasBindings()) {
myBindings.ready();
}
};
}(bindings.length -1));
},
bindLast: function(cb) {
afterBindings.push(cb);
},
ready: function(){
for(var i = 0; i < bindings.length; i += 1) {
bindings[i]();
}
bindings = [];
for(var i = 0; i < afterBindings.length; i += 1) {
afterBindings[i]();
}
afterBindings = [];
},
hasBindings: function() {
return bindings.length > 0;
}
};
return myBindings;
}
};
// Return a function that will remove this binding and
// fire the ready function if there are no more bindings
return (function(index){
return function(){
bindings[index]();
delete bindings[index];
if(!myBindings.hasBindings()) {
myBindings.ready();
}
};
}(_.size(bindings) -1));
},
bindLast: function(cb) {
afterBindings.push(cb);
},
ready: function(){
_.each(bindings, function(binding) {
bindings[i]();
});
bindings = {};
for(var i = 0; i < afterBindings.length; i += 1) {
afterBindings[i]();
}
afterBindings = [];
},
hasBindings: function() {
return _.size(bindings) > 0;
}
};
return myBindings;
}
};