Skip to content

Instantly share code, notes, and snippets.

@mloberg
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mloberg/ca473e9575f1c98a0193 to your computer and use it in GitHub Desktop.
Save mloberg/ca473e9575f1c98a0193 to your computer and use it in GitHub Desktop.
<ul data-collection-item-prototype="prototype goes here">
<li>
<label for="name">Name</label>
<input type="text" id="name" name="name">
</li>
<li>
<label for="email">Email</label>
<input type="email" id="email" name="email">
</li>
<li>
<ul class="js-collection-type" data-collection-prototype="">
<li class="js-collection-item">
<label for="book1">Book 1</label>
<input type="text" id="book1" name="books[1]">
<a href="#" class="js-collection-remove">Remove Book</a>
</li>
<li class="js-collection-placeholder">
<a href="#" class="js-collection-add">+ Add Book</a>
</li>
</ul>
</li>
<li>
<input type="submit" class="btn" value="Register">
</li>
</ul>
/**
* @fileOverview Form collection handler
*
* @author Matthew Loberg
*/
define(function(require, module, exports) {
'use strict';
var $ = require('jquery');
/**
* FormCollectionView to handle form collections
*
* @class FormCollectionView
* @constructor
*/
var FormCollectionView = function() {
this.init();
};
/**
* Initializes the UI Component View
*
* @method init
* @chainable
*/
FormCollectionView.prototype.init = function() {
var self = this;
$('.js-collection-type').not('.js-collection-type_initialized').each(function() {
var $element = $(this);
self.setup($element);
}).addClass('js-collection-type_initialized');
return this;
};
/**
* Setup form collection element
*
* @method setup
* @param {jQuery} $collectionContainer collection element
* @chainable
*/
FormCollectionView.prototype.setup = function($collectionContainer) {
$collectionContainer.data('index', $collectionContainer.find('.js-collection-item').length);
this.setupRemove($collectionContainer)
.setupAdd($collectionContainer);
};
/**
* Setup item remove
*
* @method setupRemove
* @param {jQuery} $collectionContainer collection element
* @chainable
*/
FormCollectionView.prototype.setupRemove = function($collectionContainer) {
var self = this;
$collectionContainer.find('.js-collection-item').not('.js-collection-item_initialized').each(function() {
var $element = $(this);
self.itemRemoveHandler($element);
}).addClass('js-collection-item_initialized');
return this;
};
/**
* Setup item add
*
* @method setupAdd
* @param {jQuery} $collectionContainer collection element
*/
FormCollectionView.prototype.setupAdd = function($collectionContainer) {
var self = this;
var $addLink = $collectionContainer.find('.js-collection-add');
if ($addLink) {
$addLink.on('click', function(e) {
e.preventDefault();
self.itemAddHandler($collectionContainer);
});
}
};
/**
* Setup remove handler on item
*
* @method itemRemoveHandler
* @param {jQuery} $element collection item
*/
FormCollectionView.prototype.itemRemoveHandler = function($element) {
var $removeLink = $element.find('.js-collection-remove');
if ($removeLink) {
$removeLink.on('click', function(e) {
e.preventDefault();
$element.remove();
});
}
};
/**
* Setup add handler for item
*
* @method itemAddHandler
* @param {jQuery} $collectionContainer collection container
*/
FormCollectionView.prototype.itemAddHandler = function($collectionContainer) {
var prototype = $collectionContainer.data('collection-item-prototype');
var index = $collectionContainer.data('index') + 1;
var newInput = prototype.replace(/__name__/g, index);
$collectionContainer.data('index', index);
$collectionContainer.find('.js-collection-placeholder').before(newInput);
this.setupRemove($collectionContainer);
};
return FormCollectionView;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment