Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor loadDynamicContent into slightly more digestable pieces #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions js/fpc-prototype.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var Diehard = Class.create();

Diehard.prototype =
{
initialize: function(url, action) {
Expand All @@ -23,23 +24,50 @@ Diehard.prototype =
},

loadDynamicContent: function() {
// Remove ignored blocks
var ignored = Mage.Cookies.get('diehard_ignored');
if (ignored === null) { // No cookie means user has only hit cached pages thus far
ignored = this.defaultIgnored; // Use all default ignored blocks as ignored blocks
} else if (ignored == '-') { // '-' is a sentinel value for no blocks
ignored = [];
} else { // Otherwise, if cookie is present then only ignore blocks that are in the cookie
ignored = ignored.split(',');
this._removeIgnoredBlocks();
this._fetchDynamicContent();
},

/**
* Few cases for ignored blocks:
*
* 1. No cookie means user has only hit cached pages thus far.
* In this case, we'll use all default ignored blocks as
* ignored blocks
*
* 2. '-' is a sentinal value for no blocks
*
* 3. If a cookie is present, then only ignore the blocks
* that are in the cookie.
*/
_getIgnoredBlocks: function()
{
var ignoredCookie = Mage.Cookies.get('diehard_ignored');

if (ignoredCookie === null) {
return this.defaultIgnored;
} else if (ignoredCookie == '-') {
return [];
} else {
return ignoredCookie.split(',');
}
},

_removeIgnoredBlocks: function()
{
var ignored = this._getIgnoredBlocks();
this.blocks = $H(this.blocks).inject({}, function(acc, pair){
if ( ! ignored.member(pair.value)) {
acc[pair.key] = pair.value;
}
return acc;
});

// Fetch dynamic content
return this;
},

_fetchDynamicContent: function()
{
if ($H(this.blocks).keys().length) {
var params = {
full_action_name: this.action,
Expand All @@ -55,8 +83,11 @@ Diehard.prototype =
}
});
}

return this;
}
};

Diehard.replaceBlocks = function(data) {
$H(data.blocks).each(function(block){
var matches = $$(block.key);
Expand Down