Skip to content

Commit

Permalink
#14 Dynamically updating paths in client.js based on config
Browse files Browse the repository at this point in the history
  • Loading branch information
tracend committed Jun 21, 2012
1 parent a9c798a commit e30ba24
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
21 changes: 16 additions & 5 deletions config/default.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
module.exports = config = {
"name" : "CRUDr",
"static" : {
"client.js": "/crudr/client.js"
}
}
"name" : "CRUDr",
"host" : "localhost",
"static" : {
"client.js": "/crudr/client.js",
"socket.io.js": "/socket.io/socket.io.js"
},
"oauth" : {
"authorize" : "/oauth/authorize"
},
"routes" : {
"create" : "/crudr/create",
"read" : "/crudr/read",
"update" : "/crudr/update",
"delete" : "/crudr/delete"
}
}
35 changes: 13 additions & 22 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@

// load dependencies
// - the socket.io lib
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = false;
po.src = '{{socket.io.js}}';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();

(function () {

this.CRUDr = function ( key ){
Expand All @@ -22,7 +31,7 @@

// if (window.location.hash.length == 0) {
if( typeof( cookie ) == "undefined" && typeof( query ) == "undefined" ){
var path = '/oauth/authorize?';
var path = '{{authorize}}'+'?';
var queryParams = ['client_id=' + this.key,
'redirect_uri=' + window.location,
'response_type=token'];
Expand All @@ -40,33 +49,14 @@
} else if (query == "false") {
alert("CRUDr authentication failed");
}
/*
var accessToken = window.location.hash.substring(1);
var path = "/read?";
var queryParams = [accessToken, 'callback=displayUser'];
var query = queryParams.join('&');
var url = path + query;
// use jsonp to call the graph
var script = document.createElement('script');
script.src = url;
*/


}
}

}).call(this);

// load the socket.io lib
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = false;
po.src = '/socket.io/socket.io.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();

(function() {
//var socket = io.connect();
var socket = io.connect();
//var origSync = Backbone.sync;
var socket = {};
var origSync = {};
Expand Down Expand Up @@ -100,7 +90,7 @@
});
} else {
// Call the original Backbone.sync
origSync(method, model, options);
//origSync(method, model, options);
}
};

Expand Down Expand Up @@ -175,6 +165,7 @@
};

var Helpers = {
// Backbone.js support
// Listen for backend notifications and update the
// collection models accordingly.
bindBackend: function() {
Expand Down
15 changes: 14 additions & 1 deletion lib/crudr.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,20 @@ function setupStatic( server ){
// Serve client-side code
//io.static.add('/crudr.js', { file: __dirname + '/socket.js' });
server.get( config.static["client.js"], function(req, res){
res.sendfile( __dirname + '/client.js' );
//res.sendfile( __dirname + '/client.js' );

fs.readFile( __dirname + '/client.js', function (err, data) {
if (err) throw err;
// manual replace - use a template instead ?
/*res.render('client', {
locals: {"socket.io.js": config.static["socket.io.js"]},
headers: {'content-type': 'text/javascript'}
});*/
data = data.toString().replace("{{socket.io.js}}", config.static["socket.io.js"]).replace("{{authorize}}",config.oauth.authorize);
res.write(data);
res.end();
});

});

}
Expand Down

0 comments on commit e30ba24

Please sign in to comment.