Skip to content
This repository has been archived by the owner on Oct 9, 2018. It is now read-only.

Commit

Permalink
Furthering the tests and the capabilities.
Browse files Browse the repository at this point in the history
Adding save, del.

issue #40
  • Loading branch information
stomlinson committed Jul 10, 2011
1 parent 8c8a9b8 commit c7ef5f8
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 63 deletions.
153 changes: 95 additions & 58 deletions src/collectionPluginPersistence.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
/**
* A plugin to a collection to give the collection db ops. This is part of what is usually called an Adapter
* when referring to collections with a hookup to a database. The CollectionPluginPersistence is not the actual
* Adapter but binds a collection to an Adapter. The CollectionPluginPersistence adds load, add, save, del
* functions to the collection, all four functions are assumed to operate asynchronously.
* When configuring the plugin, 4 parameters can be specified, each are optional.
* The four paramters are addCallback, saveCallback, loadCallback, and deleteCallback. When the callbacks are called, they
* will be called with two parameters, item, options. item is the item currently being operated on. options is
* options data that will contain at least two fields, collection and onComplete. onComplte should be called by the
* adapter when the adapter function
* has completed.
* A plugin to a collection to give the collection db ops. This is part of
* what is usually called an Adapter when referring to collections with a
* hookup to a database. The CollectionPluginPersistence is not the actual
* Adapter but binds a collection to an Adapter. The
* CollectionPluginPersistence adds load, add, save, del functions to the
* collection, all four functions are assumed to operate asynchronously. When
* configuring the plugin, 4 parameters can be specified, each are optional.
* The four paramters are addCallback, saveCallback, loadCallback, and
* deleteCallback. When the callbacks are called, they will be called with two
* parameters, item, options. item is the item currently being operated on.
* options is options data that will contain at least two fields, collection and
* onComplete. onComplte should be called by the adapter when the adapter
* function has completed.
*
* // set up the adapter
* var dbAdapter = {
Expand Down Expand Up @@ -107,32 +110,40 @@ AFrame.CollectionPluginPersistence = ( function() {
var Plugin = AFrame.Plugin.extend( {
init: function( config ) {
/**
* function to call to do add. Will be called with two parameters, data, and options.
* function to call to do add. Will be called with two parameters,
* data, and options.
* @config addCallback
* @type function (optional)
*/
this.addCallback = config.addCallback || noPersistenceOp;
this.addCallback = config.addCallback || this.addCallback
|| noPersistenceOp;

/**
* function to call to do save. Will be called with two parameters, data, and options.
* function to call to do save. Will be called with two parameters,
* data, and options.
* @config saveCallback
* @type function (optional)
*/
this.saveCallback = config.saveCallback || noPersistenceOp;
this.saveCallback = config.saveCallback || this.saveCallback
|| noPersistenceOp;

/**
* function to call to do load. Will be called with one parameter, options.
* function to call to do load. Will be called with one parameter,
* options.
* @config loadCallback
* @type function (optional)
*/
this.loadCallback = config.loadCallback || noPersistenceOp;
this.loadCallback = config.loadCallback || this.loadCallback
|| noPersistenceOp;

/**
* function to call to do delete. Will be called with two parameters, data, and options.
* function to call to do delete. Will be called with two
* parameters, data, and options.
* @config deleteCallback
* @type function (optional)
*/
this.deleteCallback = config.deleteCallback || noPersistenceOp;
this.deleteCallback = config.deleteCallback || this.deleteCallback
|| noPersistenceOp;

Plugin.sc.init.call( this, config );

Expand All @@ -145,9 +156,10 @@ AFrame.CollectionPluginPersistence = ( function() {


/**
* Add an item to the collection. The item will be inserted into the collection once the addCallback
* is complete. Because of this, no cid is returned from the add function, but one will be placed into
* the options item passed to the onComplete callback.
* Add an item to the collection. The item will be inserted into the
* collection once the addCallback is complete. Because of this, no
* cid is returned from the add function, but one will be placed into
* the options item passed to the onComplete callback.
*
* // Adds an item to the collection. Note, a cid is not given back
* // because this operation is asynchronous and a cid will not be
Expand All @@ -166,21 +178,24 @@ AFrame.CollectionPluginPersistence = ( function() {
* @method add
* @param {object} item - item to add
* @param {object} options - options information.
* @param {function} options.onComplete (optional) - callback to call when complete
* Will be called with two parameters, the item, and options information.
* @param {function} options.insertAt (optional) - data to be passed as second argument to the collection's
* insert function. Useful when using CollectionArrays to specify the index
* @param {boolean} options.force (optional) - If set to true, an add will be forced even if
* onBeforeAdd has its preventDefault called.
* @param {function} options.onComplete (optional) - callback to call
* when complete. Will be called with two parameters, the item, and
* options information.
* @param {function} options.insertAt (optional) - data to be passed
* as second argument to the collection's insert function. Useful when
* using CollectionArrays to specify the index
* @param {boolean} options.force (optional) - If set to true, an add
* will be forced even if onBeforeAdd has its preventDefault called.
*/
add: function( item, options ) {
options = getOptions( this, options );
var callback = options.onComplete;

var plugged = this.getPlugged();
/**
* Triggered on the collection before an add is sent to persistence. If the event has preventDefault called,
* the add will be cancelled as long as the options.force is not set to true
* Triggered on the collection before an add is sent to persistence.
* If the event has preventDefault called, the add will be cancelled
* as long as the options.force is not set to true
* @event onBeforeAdd
* @param {AFrame.Event} event - event object
* @param {variant} event.item - the item being added
Expand All @@ -190,8 +205,9 @@ AFrame.CollectionPluginPersistence = ( function() {

if( plugged.shouldDoAction( options, event ) ) {
options.onComplete = function( overriddenItem ) {
// For the insert item, use the item given by the db access layer, if they pass one back. If
// no override is given, use the original item.
// For the insert item, use the item given by the db access
// layer, if they pass one back. If no override is given,
// use the original item.
item = overriddenItem || item;
var cid = plugged.insert( item, options.insertAt );
options.cid = cid;
Expand All @@ -200,7 +216,8 @@ AFrame.CollectionPluginPersistence = ( function() {
callback && callback( item, options );

/**
* Triggered on the collection after an item is sent to persistence and is added to the Collection.
* Triggered on the collection after an item is sent to
* persistence and is added to the Collection.
* @event onAdd
* @param {AFrame.Event} event - event object
* @param {variant} event.item - the item being added
Expand Down Expand Up @@ -228,16 +245,18 @@ AFrame.CollectionPluginPersistence = ( function() {
*
* @method load
* @param {object} options - options information.
* @param {function} options.onComplete (optional) - the callback will be called when operation is complete.
* Callback will be called with two parameters, the items, and options information.
* @param {function} options.onComplete (optional) - the callback will
* be called when operation is complete. Callback will be called with
* two parameters, the items, and options information.
*/
load: function( options ) {
options = getOptions( this, options );
var plugged = this.getPlugged();

/**
* Triggered on the collection before a load occurs. If the listener calls preventDefault on the event,
* the load will be cancelled unless the load is forced.
* Triggered on the collection before a load occurs. If the listener
* calls preventDefault on the event, the load will be cancelled
* unless the load is forced.
* @event onBeforeLoad
* @param {object} event - event information
* @param {boolean} event.force - whether the load is being forced.
Expand All @@ -252,7 +271,8 @@ AFrame.CollectionPluginPersistence = ( function() {
* Triggered on the collection whenever a load is starting
* @event onLoadStart
* @param {object} event - event information
* @param {boolean} event.force - whether the load is being forced.
* @param {boolean} event.force - whether the load is
* being forced.
*/
plugged.triggerEvent( {
type: 'onLoadStart',
Expand All @@ -275,9 +295,11 @@ AFrame.CollectionPluginPersistence = ( function() {
/**
* Triggered on the collection whenever a load has completed
* @event onLoad
* @param {object} event - event information, has collection and items fields.
* @param {object} event - event information, has collection and
* items fields.
* @param {variant} event.items- items loaded inserted
* @param {boolean} event.force - whether the load is being forced.
* @param {boolean} event.force - whether the load is being
* forced.
*/
plugged.triggerEvent( {
items: items,
Expand All @@ -301,23 +323,28 @@ AFrame.CollectionPluginPersistence = ( function() {
* @method del
* @param {id || index} itemID - id or index of item to remove
* @param {object} options - options information.
* @param {function} options.onComplete (optional) - the callback will be called when operation is complete.
* Callback will be called with two parameters, the item, and options information.
* @param {function} options.onComplete (optional) - the callback will
* be called when operation is complete. Callback will be called with
* two parameters, the item, and options information.
*/
del: function( itemID, options ) {
var plugged = this.getPlugged();
var item = plugged.get( itemID );

if( item ) {
/**
* Triggered on the collection before a delete is sent to persistence. If the event has preventDefault called,
* the delete will be cancelled as long as the options.force is not set to true
* Triggered on the collection before a delete is sent to
* persistence. If the event has preventDefault called, the
* delete will be cancelled as long as the options.force is not
* set to true
* @event onBeforeDelete
* @param {AFrame.Event} event - event object
* @param {variant} event.item - the item being deleted
* @param {boolean} event.force - whether the delete is being forced.
* @param {boolean} event.force - whether the delete is being
* forced.
*/
var event = plugged.triggerEvent( getEvent( 'onBeforeDelete', item, options ) );
var event = plugged.triggerEvent( getEvent( 'onBeforeDelete',
item, options ) );

if( plugged.shouldDoAction( options, event ) ) {
options = getOptions( this, options );
Expand All @@ -332,11 +359,13 @@ AFrame.CollectionPluginPersistence = ( function() {
this.deleteCallback( item, options );

/**
* Triggered on the collection after an item is deleted from persistence and is removed from the Collection.
* Triggered on the collection after an item is deleted from
* persistence and is removed from the Collection.
* @event onDelete
* @param {AFrame.Event} event - event object
* @param {variant} event.item - the item being deleted
* @param {boolean} event.force - whether the delete is being forced.
* @param {boolean} event.force - whether the delete is being
* forced.
* @param {id} event.cid - item's cid
*/
plugged.triggerEvent( getEvent( 'onDelete', item, options ) );
Expand All @@ -358,23 +387,28 @@ AFrame.CollectionPluginPersistence = ( function() {
* @method save
* @param {id || index} itemID - id or index of item to save
* @param {object} options - options information.
* @param {function} options.onComplete (optional) - the callback will be called when operation is complete.
* Callback will be called with two parameters, the item, and options information.
* @param {function} options.onComplete (optional) - the callback will
* be called when operation is complete. Callback will be called with
* two parameters, the item, and options information.
*/
save: function( itemID, options ) {
var plugged = this.getPlugged();
var item = plugged.get( itemID );

if( item ) {
/**
* Triggered on the collection before a save is sent to persistence. If the event has preventDefault called,
* the save elete will be cancelled as long as the options.force is not set to true
* Triggered on the collection before a save is sent to
* persistence. If the event has preventDefault called, the save
* will be cancelled as long as the options.force is not set to
* true
* @event onBeforeSave
* @param {AFrame.Event} event - event object
* @param {variant} event.item - the item being saved
* @param {boolean} event.force - whether the save is being forced.
* @param {boolean} event.force - whether the save is being
* forced.
*/
var event = plugged.triggerEvent( getEvent( 'onBeforeSave', item, options ) );
var event = plugged.triggerEvent( getEvent( 'onBeforeSave',
item, options ) );
if( plugged.shouldDoAction( options, event ) ) {

options = getOptions( this, options );
Expand All @@ -388,11 +422,13 @@ AFrame.CollectionPluginPersistence = ( function() {
this.saveCallback( item, options );

/**
* Triggered on the collection after an item is saved to persistence.
* Triggered on the collection after an item is saved to
* persistence.
* @event onSave
* @param {AFrame.Event} event - event object
* @param {variant} event.item - the item being saved
* @param {boolean} event.force - whether the save is being forced.
* @param {boolean} event.force - whether the save is being
* forced.
* @param {id} event.cid - item's cid
*/
plugged.triggerEvent( getEvent( 'onSave', item, options ) );
Expand All @@ -413,7 +449,8 @@ AFrame.CollectionPluginPersistence = ( function() {
}

/**
* Get an event object. Used when triggering the on[Add|Delete|Save|Load]* events
* Get an event object. Used when triggering the
* on[Add|Delete|Save|Load]* events
* @method getEvent
* @private
*/
Expand All @@ -435,8 +472,8 @@ AFrame.CollectionPluginPersistence = ( function() {
}

/**
* A NoOp type function that just calls the onComplete function, used for persistence functions
* where no callback is specified.
* A NoOp type function that just calls the onComplete function, used for
* persistence functions where no callback is specified.
* @method noPersistenceOp
* @private
*/
Expand Down
23 changes: 21 additions & 2 deletions src/collectionPluginREST.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AFrame.CollectionPluginREST = (function() {
var Plugin = AFrame.CollectionPluginPersistence.extend({
var Plugin = AFrame.CollectionPluginPersistence.extend( {
importconfig: [ 'root', 'net' ],
loadCallback: function( options ) {
var me=this;
Expand All @@ -17,8 +17,27 @@ AFrame.CollectionPluginREST = (function() {
type: 'POST',
success: options.onComplete
} );
},

deleteCallback: function( item, options ) {
var me=this;
me.net.ajax( {
url: me.root + '/' + item.id,
type: 'DEL',
success: options.onComplete
} );
},

saveCallback: function( item, options ) {
var me=this;
me.net.ajax( {
url: me.root + '/' + item.id,
data: item,
type: 'PUT',
success: options.onComplete
} );
}
});
} );

return Plugin;
}());
2 changes: 2 additions & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<script type="text/javascript" src="../src/dataValidation.js"></script>
<script type="text/javascript" src="../src/model.js"></script>
<script type="text/javascript" src="../src/event.js"></script>
<script type="text/javascript" src="../src/collectionPluginREST.js"></script>


<script type="text/javascript" src="test_aframe.js"></script>
Expand Down Expand Up @@ -76,6 +77,7 @@
<script type="text/javascript" src="test_event.js"></script>
<script type="text/javascript" src="test_enumerableMixin.js"></script>
<script type="text/javascript" src="test_dom.js"></script>
<script type="text/javascript" src="test_collectionPluginREST.js"></script>

<!-- this has to go last -->
<script type="text/javascript" src="test_harness_end.js"></script>
Expand Down
Loading

0 comments on commit c7ef5f8

Please sign in to comment.