What's new? | Help | Directory | Sign in
Google
  
  
  
  
    
Search
for
Updated Aug 29, 2007 by steve.yen
TrimJunctionHowItWorks  

How Does Junction Work

{ Junction home | documentation }

Junction decorates your functions and prototypes dynamically. It's like aspect-oriented-programming. The decorating happens at the moment when you give Junction any meta information or declarations about your application.

For example, let's take a look at the code for a sample Milestone model object, which could be part of a Project-Milestone tracking application...

function Milestone() {}

modelInit('Milestone');

Milestone.belongsTo('Creator',  { modelName : 'Person' });
Milestone.belongsTo('Domain',   { modelName : 'Org' });
Milestone.belongsTo('Project');
Milestone.belongsTo('Assignee', { modelName : 'Person' });

Milestone.hasMany('Messages');
Milestone.hasMany('TodoLists');

Milestone.findRecentByProject = function(projectId, date) {
    return Milestone.find('all', {
      conditions: [ "Milestone.project_id = ? AND Milestone.created_at > ?",
                    projectId, date ] });
}

When '''modelInit()''' is invoked, Junction knows that the Milestone function is to be a model object in the Model-View-Controller sense. The '''modelInit()''' function performs the following...

Back to the sample Milestone code, when the '''belongsTo()''' declaration function is called, it adds even more functions to the Milestone's prototype object. For example, when ''belongsTo('Creator', { modelName : 'Person' })'' is invoked, the following happens...

Similarly, when the '''hasMany()''' declaration function is called, such as ''hasMany('Messages')'', Junction adds methods to the Milestone.prototype object such as getMessages().

{ Junction home | documentation }