node-barefoot
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -30,3 +30,3 @@ /** Mixin: Barefoot.APIAdapter.Server | ||
* argument when executing the callback | ||
* (Function) successHandler - A function which is injected as the second | ||
* (Function) errorHandler - A function which is injected as the second | ||
* argument when executing the callback | ||
@@ -271,7 +271,32 @@ * (Function) callback - The actual callback function you want to execute | ||
/** Function: dispatchApiCall | ||
/** Function: dispatchLocalApiCall | ||
* This is the core where server side API callbacks are dispatched. It is called | ||
* by Barefoots <sync> replacement. | ||
* | ||
* It tries to match the given url with a registered route for the given | ||
* httpMethod. If found, the route callback is invoked. | ||
* | ||
* If the data argument contains any information, that stuf gets passed to the | ||
* callback. If the options argument contains a success or error element, these | ||
* functions are called by the callback in case of success or failure. | ||
* | ||
* Example Call: | ||
* > dispatchLocalApiCall('post', '/contacts', { name: foo }, { | ||
* > success: function() { console.log('yay!'); } | ||
* > , error: function() { console.log('nay :('); } | ||
* > }); | ||
* | ||
* Parameters: | ||
* (String) httpMethod - An HTTP verb. Necessary to match a registered | ||
* route. | ||
* (String) url - The URL of the API callback to dispatch | ||
* (Object) data - Optional. Contains information which should be passed to | ||
* the callback. (Example: Contact information which should | ||
* be saved.) | ||
* (Object) options - Should contain an error and success callback function. | ||
*/ | ||
function dispatchApiCall(httpMethod, url, options) { | ||
function dispatchLocalApiCall(httpMethod, url, data, options) { | ||
var params = {} | ||
, matchedRoute = matchRoute(httpMethod, url, this.apiRoutes, params); | ||
options = options || {}; | ||
@@ -283,9 +308,13 @@ if(_.isUndefined(matchedRoute)) { | ||
var success = function success(apiResult) { | ||
if(options.success) { options.success(apiResult); } | ||
if(_.has(options, 'success')) { options.success(apiResult); } | ||
} | ||
, error = function error(err) { | ||
if(options.error) { options.error(err); } | ||
if(_.has(options, 'error')) { options.error(err); } | ||
} | ||
, args = _.union(success, error, _.values(params)); | ||
, args = _.union( | ||
success | ||
, error | ||
, _.values(params) | ||
, data | ||
); | ||
try { | ||
@@ -296,9 +325,7 @@ matchedRoute.callback.apply(this, args); | ||
} | ||
} | ||
/** Function: sync | ||
* This is the core where server side API callbacks are dispatched. This | ||
* function replaces Backbone.sync and gets in place during startup | ||
* (<Barefoot.Startup.Server>). | ||
* During startup on the server, this function replaces Backbones own sync | ||
* implementation to shortcut "local" API calls. | ||
* | ||
@@ -309,2 +336,5 @@ * Instead going the detour over an AJAX request, this implementation of sync | ||
* | ||
* If method is not equal to read or delete, the return value of the toJSON | ||
* function of the given model is passed to the API callback. | ||
* | ||
* Parameters: | ||
@@ -321,3 +351,4 @@ * (String) method - A method (create, update, patch, delete or read) which | ||
var url = options.url || _.result(model, 'url') | ||
, httpMethod = methodMap[method]; | ||
, httpMethod = methodMap[method] | ||
, data; | ||
@@ -328,3 +359,7 @@ if(_.isUndefined(url)) { | ||
this.dispatchApiCall(httpMethod, url, options); | ||
if(method !== 'read' && method !== 'delete') { | ||
data = model.toJSON(); | ||
} | ||
this.dispatchLocalApiCall(httpMethod, url, data, options); | ||
} | ||
@@ -335,4 +370,4 @@ | ||
, createRouteFactories: createRouteFactories | ||
, dispatchApiCall: dispatchApiCall | ||
, dispatchLocalApiCall: dispatchLocalApiCall | ||
, sync: sync | ||
}; |
@@ -36,16 +36,22 @@ /** Mixin: Barefoot.Start.Server | ||
function prepareBrowserify(app, options) { | ||
if(!_.has(options, 'file') || !_.has(options, 'route')) { | ||
throw new Error('Missing "file" and "route" property.'); | ||
if(!_.has(options, 'mainFile') || !_.has(options, 'url')) { | ||
throw new Error('Missing "mainFile" and "url" property.'); | ||
} | ||
var exclude = options.exclude || []; | ||
var mainFile = options.mainFile | ||
, url = options.url | ||
, exclude = options.exclude || [] | ||
, browserifyOptions; | ||
exclude = exclude.concat(getServerOnlyFiles()); | ||
browserifyOptions = { ignore: exclude }; | ||
_.extend(browserifyOptions, _.omit(options, ['mainFile', 'exclude'])) | ||
app.get(options.route, browserify( | ||
options.file, { | ||
ignore: exclude | ||
//, debug: false | ||
//, gzip: true | ||
//, minify: true | ||
})); | ||
_.defaults(browserifyOptions, { | ||
debug: false | ||
, gzip: true | ||
, minify: true | ||
}); | ||
app.get(url, browserify(mainFile, browserifyOptions)); | ||
} | ||
@@ -52,0 +58,0 @@ |
{ | ||
"name": "node-barefoot" | ||
, "version": "0.0.1" | ||
, "version": "0.0.2" | ||
, "description": "Barefoot makes code sharing between browser and server reality. Write your application once and run it on both ends of the wire." | ||
@@ -5,0 +5,0 @@ , "keywords": [ |
@@ -24,2 +24,2 @@ # Barefoot | ||
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/swissmanu/barefoot/trend.png)](https://bitdeli.com/free "Bitdeli Badge") | ||
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/swissmanu/barefoot/trend.png)](https://bitdeli.com/free "Bitdeli Badge") [![githalytics.com alpha](https://cruel-carlota.pagodabox.com/f87c96d81337e0f3f6a255aedc521c76 "githalytics.com")](http://githalytics.com/swissmanu/barefoot) |
describe('Start.Server', function() { | ||
var should = require('chai').should() | ||
, RouterMock = function(){ return { start: function(){} }} | ||
, appMock = { | ||
get: function() {} | ||
}; | ||
, appMock = require('../mocks/expressjs/app') | ||
, RouterMock = function(){ return { start: function(){} }}; | ||
@@ -22,3 +20,3 @@ it('should throw an error if no express.js app is passed with startOptions', function() { | ||
app: appMock | ||
, mainJavaScriptFile: { file: '', route: '' } | ||
, mainJavaScriptFile: { mainFile: '', url: '' } | ||
, setupMiddlewares: function() { done(); } | ||
@@ -25,0 +23,0 @@ }; |
97311
50
2233