< documentation
Plugins
Extension Points
filters 1.0.x
usage
Filters are defined by passing an array of individual filters to the $.filter plugin. Filters are merged so the plugin may be used many times to add filters using the same method. By convention it is recommended that filters be defined in app/configs/filters.js so other developers will know where to look for them, and the entire file be defined within it's own closure.
(function($){ $.filters([ /** { individual filter configurations here }, { and here }, { and here }, { etc } */ ]) })(jQuery);
common_options
In general, individual filter configurations support some very powerful regular expression syntax, so you can apply a filter across an entire namespace (called the target), and on any functions of the target that match the given expression (specified by around, before, or after). Each type of filter is defined below:
Name | Type | |
---|---|---|
id | String | |
A unique id for the filter across the application | ||
target | String | |
Either the name of a global function, a dot-delimited name of a class (as a string), an application managed object id (prefixed with 'ref://'), or a namespace followed by ".*", which implies the filter will be applied to every object in the namespace. (see example below for illustration) | ||
around|before|after | String | |
Treated as a RegExp, you can apply the filter to multiple methods of the target. |
around
Around is type of filter that allows you to intercept a function call before it is made, inspect the arguments, and either allow the function call to continue naturally, or prevent the function being called, perhaps calling another function in its place. You can also then inspect the returned value and either modify it, return it, or return another value entirely.
Name | Type | |
---|---|---|
advice | Function | |
The advice is the function that will be called at the interception point. this function will have the argument: 'invocation | ||
invocation.proceed | function | |
You must call invocation.proceed to allow the intercepted function to be called as expected. | ||
invocation.arguments | function | |
is the array of args as presented to the intercepted function | ||
invocation.object | function | |
is a refernece to the object the intercepted function was called on. |
In the following example we have a data access object that has a requirement of needing a valid session id in order for ajax calls to happen successfully. we simply intercept the call, establish a session if it has timed out, and proceed with the call as usual
/* * @file app/configs/filters.js * @description If you need to intercept calls to portions * of you application and modify the behavior based on * state or other conditions, filters are the place to do it */ (function($){ var contactModel; $.filters([{ id : "session-filter", target : "MyApp.Models.Contact", around : "(find|save|create|delete)", advice : function(invocation){ contactModel = contactModel||$.$("#contactModel"); if(contactModel && contactModel.sessionExpired){ $.$("#searchController").init(function(session){ if(session){ invocation.proceed(); }else{ alert('session invalid, please login'); } }); }else{ alert('session invalid, please login'); } } }]); })(jQuery);
before
'before' filters allow you to intercept a collection of function calls and optionally modify the arguments passed to them.
Name | Type | |
---|---|---|
before | String | |
Treated as a RegExp, you can apply the filter to multiple methods of the target. |
In the following example we are intercepting a call to a particular view rendering and passing some additional information and sorting the name alphabetically.
/* * @file app/configs/filters.js * @description If you need to intercept calls to portions * of you application and modify the behavior based on * state or other conditions, filters are the place to do it */ (function($){ var userModel; $.filters([{ id : "user-decoration-filter", target : "MyApp.Views.Contacts", before : "(update)", advice : function(){ userModel = userModel||$.$("#userModel"); userModel.sort(arguments[0], arguments[0].sortMode); $.extend(true, arguments[0], { lastAdded:userModel.lastAdded }); } }]); })(jQuery);
after
After filters allow you to intercept the return value of a set of functions and either modify them or just use them to trigger additional events etc.
Name | Type | |
---|---|---|
after | String | |
Treated as a RegExp, you can apply the filter to multiple methods of the target. |
In the following example we are intercepting a the return value of a network call that indicates if the submission is successful. Based on the return value we trigger additional events.
/* * @file app/configs/filters.js * @description If you need to intercept calls to portions * of you application and modify the behavior based on * state or other conditions, filters are the place to do it */ (function($){ $.filters([{ id : "status-filter", target : "MyApp.Models.Search", after : "(submit)", advice : function(retval){ if(retval > 0){ $(document).trigger('searching'); }else{ $(document).trigger('network-unavailable'); } } }]); })(jQuery);
Project
- app
- app/boot
- app/boot/client.js
- app/boot/server.js
- app/configs
- app/configs/config.js
- app/configs/environments.js
- app/configs/filters.js
- app/configs/logging.js
- app/configs/routes.js
- app/controllers
- app/controllers/example.js
- app/models
- app/models/example.js
- app/services
- app/services/example.js
- app/templates
- app/templates/example.js
- app/views
- app/views/example.js
- index.html
Guides
This guide is applicable to both the jquery-claypool client and server application frameworks. Where the two differ functionally the documentation will provide notes and examples of usage in each environment.