Applications are a powerful abstraction – ideally, when writing a site using the Wilson framework, you will build it out of several apps that provide discrete functionality. Apps are patterned after Django’s apps – that is, they provide models, views, routes, middleware, and templates.
Where they diverge, however, is that applications by themselves are meaningless until your site-wide settings registers them with a site-specific name. You cannot use their models, you cannot meaningfully use their views, etc, until they are registered. You can install an app as many times at as many routes as you like, and you can reopen an app to override settings within it. This is very powerful – and like many powerful things, it can also be fairly confusing to newcomers.
When you write an app, the most important piece is the App Declaration, which should be located in yourapp/index.js. It looks like this:
var application = require('wilson/application'),
app = application.app,
path = require('path'),
primary = application.primary;
exports.app = app({
'provides':'blog',
'models':require('yourapp/models'),
'external_apps':{
'auth':primary('auth')
},
'urls':require('yourapp/urls').patterns,
'template_directories':[
path.join(__dirname, 'templates')
],
'settings':{
'is_this_blog_cool':true
},
'middleware':require('yourapp/middleware')
});
As you can see, the exports.app = app({...}) declaration in index.js defines how your application appears to the outside world.
In your app/views.js, your views may look something like this:
exports.user_list = function(request) {
var User = this.externals.auth.models.User;
User.objects.all(function(objects, err) {
if(err || objects.length < 1) {
request.respond(new Http404());
} else {
renderToResponse(request)('blog/user_list.html', {
'user_list':objects
});
}
});
};
This view is executed as a method of your application instance. When working within an application instance, the externals you defined in your app delcaration are available as full-blown application instances under this.externals.<internal_name>, and their models are located at this.externals.<internal_name>.models.<ModelName>.
These models are usable instances of pieshop.resource – they can be used to query the database at this point. This decouples separate applications from one another, and makes top-level injection or swapping of two apps that provide the same functionality in different ways easy.
Attributes available on an application instance:
These are generally not advisable for discrete applications, but if you need access to an app instance whose name is known, you can do the following:
var application = require('wilson/application'),
appInstance = application.getApplicationInstance('known_app_instance_name');
Or:
appInstances = application.getApplicationInstances();
Which return a dict of appInstanceName -> appInstance.