Skip to content

Instantly share code, notes, and snippets.

@viccherubini
Created March 10, 2014 21:39
Show Gist options
  • Save viccherubini/9474952 to your computer and use it in GitHub Desktop.
Save viccherubini/9474952 to your computer and use it in GitHub Desktop.
Testable JavaScript
(function($app, $jQuery) {
"use strict";
var CampaignGroupCollection = function(options) {
this.init(options);
};
CampaignGroupCollection.prototype.init = function(options) {
this.collection = options.collection;
this.campaignGroups = [];
this.createCampaignGroups();
};
CampaignGroupCollection.prototype.createCampaignGroups = function() {
this.collection.each($jQuery.proxy(function(i, el) {
this.campaignGroups.push(new $app.CampaignGroup({
campaignGroup: el
}));
}, this));
};
$app.CampaignGroupCollection = CampaignGroupCollection;
})(this, this.jQuery);
(function($app, $jQuery) {
"use strict";
var CampaignGroup = function(options) {
this.init(options);
};
CampaignGroup.prototype.init = function(options) {
var campaignGroup = $jQuery(options.campaignGroup);
this.updateStatus = campaignGroup.find('[data-campaign-group-status]');
campaignGroup.on('blur', '[data-campaign-group-label]', $jQuery.proxy(this.updateLabel, this));
campaignGroup.on('change', '[data-campaign-group-filter]', $jQuery.proxy(this.addFilter, this));
campaignGroup.on('click', '[data-campaign-group-delete-filter]', $jQuery.proxy(this.deleteFilter, this));
};
CampaignGroup.prototype.updateLabel = function(e) {
var form = $jQuery(e.target).parent();
$jQuery.post(form.attr('action'), form.serialize());
};
CampaignGroup.prototype.addFilter = function(e) {
var form = $jQuery(e.target).parent();
VSM.drawLoader(this.updateStatus, true);
$jQuery.post(form.attr('action'), form.serialize(), $jQuery.proxy(function(html) {
form.siblings('[data-campaign-group-filter]').replaceWith(html);
VSM.hideLoader(this.updateStatus);
}, this));
};
CampaignGroup.prototype.deleteFilter = function(e) {
var el = $jQuery(e.target);
var form = el.parents('form');
VSM.drawLoader(this.updateStatus, true);
$jQuery.post(el.attr('data-action'), form.serialize(), $jQuery.proxy(function(html) {
form.replaceWith(html);
VSM.hideLoader(this.updateStatus);
}, this));
};
$app.CampaignGroup = CampaignGroup;
})(this, this.jQuery);
new this.CampaignGroupCollection({
collection: jQuery('[data-campaign-group]')
});
@zacharydanger
Copy link

I call 💩 on this one, man.

var lololololol = null;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment