Skip to content

Commit

Permalink
#37 Adding dynamic methods in routes/sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
tracend committed Jan 14, 2013
1 parent 70813b6 commit f3a4762
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 30 deletions.
44 changes: 15 additions & 29 deletions lib/crudr.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,65 +105,51 @@ CRUDr.prototype = {
},
// include dbs / backends
add: function ( options ) {

//console.log( "----- add ------");

var db = options.db || false;
var backends = options.backends || false;
// stop if there's no db or backend
if( !db || !backends ) return;
// create new backends
var backends = this.backends( options );
var dbs = this.backends( options );
// extend the existing db object
this.db = _.extend( this.db, backends);
this.db = _.extend( this.db, dbs);
// update routes
//console.log( backends );
this.routes.add( dbs );
// update sockets
this.sockets.events( backends );
this.sockets.db = this.db;
this.sockets.add( dbs );

},
// update an existing backend
update : function( options ){

console.log( "----- update ------");

//console.log( "----- update ------");
var db = options.db || false;
var backends = options.backends || false;
// stop if there's no db or backend
if( !db || !backends ) return;
// create new backends
var backends = this.backends( options );
// extend the existing db object
//console.log( backends );
this.db = _.extend( this.db, backends);
var dbs = this.backends( options );
// extend the existing db object (replacing the old dbs)
this.db = _.extend( this.db, dbs);
// update routes
//console.log( backends );
this.routes.update( dbs );
// update sockets
this.sockets.events( backends );
this.sockets.db = this.db;
this.sockets.update( dbs );

},
// remove a backend
remove: function ( options ) {

//console.log( "----- remove ------");

var db = options.db || false;
var backends = options.backends || false;
// stop if there's no db or backend
if( !db || !backends ) return;
// create new backends
var backends = this.backends( options );

for( var i in backends ){
delete this.db[i];
}
// remove events from sockets


//update dbs
this.sockets.db = this.db;
var dbs = this.backends( options );
// update routes
this.routes.remove( dbs );
// update sockets
this.sockets.remove( dbs );

}

Expand Down
37 changes: 36 additions & 1 deletion lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,49 @@ Routes.prototype = {
// app name
var name = this.options.name;

for(i in this.options.backends){
console.log( domain );
//console.log( this.db );
//for(i in this.options.backends){
for(i in this.db){
// check if the domain exists
if ( i == domain ) return domain;
// alternatively check the name-domain
if ( i == name +"_"+ domain ) return name +"_"+ domain;
}
// otherwise return false
return false;
},


add : function( db ){
// update the db list
for( var i in db ){
this.db[i] = db[i];
}
// anything else?
},

update : function( db ){

// only add the new events
for( var i in db ){
// ...
this.db[i] = db[i];
}

},

remove : function( db ){

// remove from the local db
for( var i in db ){
if( typeof this.db[i] != "undefined" ){
// remove from db
delete this.db[i];
}
}
// anything else?

}

}
Expand Down
44 changes: 44 additions & 0 deletions lib/sockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,50 @@ Sockets.prototype = {
});
});

},
// remove event listeners for domain
unbind : function( domain ){

this.io.of(domain).removeAllListeners('connection');

},

add : function( db ){
// update the db list
for( var i in db ){
this.db[i] = db[i];
}
// add new events
this.events( db );
},

update : function( db ){

// only add the new events
if( this.db[i] )
for( var i in db ){
// remove events for specific domain
if( typeof this.db[i] != "undefined" ){
this.unbind( i );
}
this.db[i] = db[i];
}
// add new events
this.events( db );

},

remove : function( db ){

for( var i in db ){
// remove events for specific domain
if( typeof this.db[i] != "undefined" ){
this.unbind( i );
// remove from db
delete this.db[i];
}
}

}

}

0 comments on commit f3a4762

Please sign in to comment.