1
0
Fork 0

Foxx: Breaking API Changes 💣

* Removed the old concept of models entirely.
* Manifest Files
  * models has been removed
  * Uses lib now as base path for all requires
* FoxxApplication is now called Application
* Changes to Application:
  * requiresLibs is now requires
  * requiresModels has been removed
This commit is contained in:
Lucas Dohmen 2013-04-03 14:55:51 +02:00
parent fee2bb2651
commit 0b7b34d09b
3 changed files with 34 additions and 53 deletions

View File

@ -14,9 +14,9 @@ So given you want to build an application that sends a plain-text response "Work
First, create a directory `my_app` and save a file called `app.js` in this directory. Write the following content to this file:
FoxxApplication = require("org/arangodb/foxx").FoxxApplication;
Foxx = require("org/arangodb/foxx");
app = new FoxxApplication();
app = new Foxx.Application();
app.get("/wiese", function(req, res) {
res.set("Content-Type", "text/plain");
@ -39,19 +39,19 @@ Now your application is done. Start ArangoDB as follows:
Now point your browser to `/my/wiese` and you should see "Worked!". After this short overview, let's get into the details.
## Details on FoxxApplication
## Details on Foxx.Application
#### new FoxxApplication
#### new Foxx.Application
@copydetails JSF_foxx_application_initializer
#### FoxxApplication#start
#### Foxx.Application#start
@copydetails JSF_foxx_application_start
#### `requiresLibs` and `requiresModels`
#### Foxx.Application#requires
Using the base paths defined in the manifest file, you can require libraries (and the special models described later) that you need in this FoxxApplication. So for example:
Using the base paths defined in the manifest file, you can require modules that you need in this FoxxApplication. So for example:
app.requiresLibs = {
app.requires = {
"schafspelz": "wolf"
};
@ -67,22 +67,22 @@ This will require the file `wolf.js` in the libs folder you have defined and mak
If you do not redefine it, all requests that go to the root of your application will be redirected to `index.html`.
#### FoxxApplication#head
#### Foxx.Application#head
@copydetails JSF_foxx_application_head
#### FoxxApplication#get
#### Foxx.Application#get
@copydetails JSF_foxx_application_get
#### FoxxApplication#post
#### Foxx.Application#post
@copydetails JSF_foxx_application_post
#### FoxxApplication#put
#### Foxx.Application#put
@copydetails JSF_foxx_application_put
#### FoxxApplication#patch
#### Foxx.Application#patch
@copydetails JSF_foxx_application_patch
#### FoxxApplication#delete
#### Foxx.Application#delete
@copydetails JSF_foxx_application_delete
### Documenting and Constraining the Routes
@ -113,30 +113,20 @@ Furthermore you can describe your API by chaining the following methods onto you
You can use the following two functions to do something before or respectively after the normal routing process is happening. You could use that for logging or to manipulate the request or response (translate it to a certain format for example).
#### FoxxApplication#before
#### Foxx.Application#before
@copydetails JSF_foxx_application_before
#### FoxxApplication#after
#### Foxx.Application#after
@copydetails JSF_foxx_application_after
### More functionality
#### FoxxApplication#helper
#### Foxx.Application#helper
@copydetails JSF_foxx_application_helper
#### FoxxApplication#accepts
#### Foxx.Application#accepts
@copydetails JSF_foxx_application_accepts
## Models
If you do not require a module with `requiresLibs`, but instead use `requiresModels`, you will have three additional functions available in the module (otherwise it will behave the same):
* `var a = requireModel("module")`: Requires the model "module" (which again will have these functions available) and makes it available via the variable `a`.
* `appCollection("test")`: Returns the collection "test" prefixed with the application prefix
* `appCollectionName("test")`: Returns the name of the collection "test" prefixed with the application prefix
If you need access to a normal module from this model, just require it as usual.
## The Request and Response Objects
When you have created your FoxxApplication you can now define routes on it. You provide each with a function that will handle the request. It gets two arguments (four, to be honest. But the other two are not relevant for now):
@ -187,8 +177,7 @@ In the `manifest.json` you define the components of your application: The conten
* `description`: A short description of the application (Meta information)
* `thumbnail`: Path to a thumbnail that represents the application (Meta information)
* `apps`: Map routes to FoxxApplications
* `lib`: Base path for the models you want to require
* `models`: Base path for the models you want to require
* `lib`: Base path for all required modules
* `files`: Deliver files
* `assets`: Deliver pre-processed files
* `system`: Mark an application as a system application
@ -209,7 +198,6 @@ A more complete example for a Manifest file:
},
"lib": "lib",
"models": "models",
"files": {
"/images": "images"

View File

@ -3,7 +3,7 @@ require("internal").flushModuleCache();
var jsunity = require("jsunity"),
console = require("console"),
arangodb = require("org/arangodb"),
FoxxApplication = require("org/arangodb/foxx").FoxxApplication,
FoxxApplication = require("org/arangodb/foxx").Application,
db = arangodb.db;
function CreateFoxxApplicationSpec () {
@ -202,12 +202,9 @@ function SetRoutesFoxxApplicationSpec () {
var myFunc = function () {},
routes = app.routingInfo.routes;
app.requiresLibs = {
app.requires = {
a: 1
};
app.requiresModels = {
b: 2
};
app.get('/simple/route', myFunc);
app.start({
context: "myContext"
@ -215,7 +212,6 @@ function SetRoutesFoxxApplicationSpec () {
assertEqual(app.routingInfo.routes[0].action.context, "myContext");
assertEqual(app.routingInfo.routes[0].action.requiresLibs.a, 1);
assertEqual(app.routingInfo.routes[0].action.requiresModels.b, 2);
}
};
}

View File

@ -28,7 +28,7 @@
/// @author Copyright 2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
var FoxxApplication,
var Application,
RequestContext,
BaseMiddleware,
FormatMiddleware,
@ -75,19 +75,19 @@ internal.createUrlObject = function (url, constraint, method) {
/// @fn JSF_foxx_application_initializer
/// @brief Create a new Application
///
/// And that's FoxxApplication. It's a constructor, so call it like this:
/// And that's Application. It's a constructor, so call it like this:
/// It takes two optional arguments as displayed above:
/// * **The URL Prefix:** All routes you define within will be prefixed with it
/// * **The Template Collection:** More information in the template section
///
/// @EXAMPLES
/// app = new FoxxApplication({
/// app = new Application({
/// urlPrefix: "/wiese",
/// templateCollection: "my_templates"
/// });
////////////////////////////////////////////////////////////////////////////////
FoxxApplication = function (options) {
Application = function (options) {
'use strict';
var urlPrefix, templateCollection, myMiddleware;
@ -100,8 +100,7 @@ FoxxApplication = function (options) {
routes: []
};
this.requiresLibs = {};
this.requiresModels = {};
this.requires = {};
this.helperCollection = {};
this.routingInfo.urlPrefix = urlPrefix;
@ -122,7 +121,7 @@ FoxxApplication = function (options) {
];
};
_.extend(FoxxApplication.prototype, {
_.extend(Application.prototype, {
////////////////////////////////////////////////////////////////////////////////
/// @fn JSF_foxx_application_start
/// @brief Start the application
@ -136,8 +135,7 @@ _.extend(FoxxApplication.prototype, {
start: function (context) {
'use strict';
var models = this.requiresModels,
requires = this.requiresLibs,
var requires = this.requires,
prefix = context.prefix;
this.routingInfo.urlPrefix = prefix + "/" + this.routingInfo.urlPrefix;
@ -145,7 +143,6 @@ _.extend(FoxxApplication.prototype, {
_.each(this.routingInfo.routes, function (route) {
route.action.context = context.context;
route.action.requiresLibs = requires;
route.action.requiresModels = models;
});
context.routingInfo = this.routingInfo;
@ -672,7 +669,7 @@ BaseMiddleware = function (templateCollection, helperCollection) {
/// @fn JSF_foxx_BaseMiddleware_response_render
/// @brief The mysterious `render` function
///
/// If you initialize your FoxxApplication with a `templateCollection`,
/// If you initialize your Application with a `templateCollection`,
/// you're in luck now.
/// It expects documents in the following form in this collection:
///
@ -689,14 +686,14 @@ BaseMiddleware = function (templateCollection, helperCollection) {
/// from this call. And with the `templateLanguage` you can choose
/// your template processor. There is only one choice now: `underscore`.
///
/// If you call render, FoxxApplication will look
/// If you call render, Application will look
/// into the this collection and search by the path attribute.
/// It will then render the template with the given data:
///
/// @EXAMPLES
/// response.render("high/way", {username: 'FoxxApplication'})
/// response.render("high/way", {username: 'Application'})
///
/// Which would set the body of the response to `hello FoxxApplication` with the
/// Which would set the body of the response to `hello Application` with the
/// template defined above. It will also set the `contentType` to
/// `text/plain` in this case.
///
@ -822,10 +819,10 @@ FormatMiddleware = function (allowedFormats, defaultFormat) {
};
};
/// We finish off with exporting FoxxApplication and the middlewares.
/// We finish off with exporting Application and the middlewares.
/// Everything else will remain our secret.
exports.FoxxApplication = FoxxApplication;
exports.Application = Application;
exports.BaseMiddleware = BaseMiddleware;
exports.FormatMiddleware = FormatMiddleware;