Skip to content

Instantly share code, notes, and snippets.

@adorsk
Created July 29, 2012 19:30
Show Gist options
  • Save adorsk/3201295 to your computer and use it in GitHub Desktop.
Save adorsk/3201295 to your computer and use it in GitHub Desktop.
facets state init
require([
"jquery",
"use!backbone",
"use!underscore",
"_s",
"use!ui",
"Facets"
],
function($, Backbone, _, _s, ui, Facets){
var facets = {};
var facet_collection_model = new Backbone.Collection();
var facet_collection_view = new Facets.views.FacetCollectionView({
el: $('#facets'),
model: facet_collection_model
});
var facetDefinitions = {
'f1': {
'id': 'f1',
'label': 'f1_label',
'type': 'list'
}
};
var facetsState = {
facetStates : [
{
'id': 'f1',
'actions': [
'create',
'getData',
{ action: 'selectChoices', byIndices: [0, 2] }
]
}
]
};
var getFakeListFacetChoices = function(){
var numChoices = 5;
var choices = [];
for (var i = 0; i < numChoices; i++){
var count = Math.random();
choices.push({
id: i,
label: "Choice_" + i,
count: count,
count_label: count
});
}
return choices;
};
var createListFacet = function(facetDef){
var model = new Backbone.Model(_.extend({}, facetDef));
var view = new Facets.views.ListFacetView({
model: model
})
model.getData = function(){
return $.Deferred(function(){
console.log("Getting Data");
var _this = this;
setTimeout(function(){
var choices = getFakeListFacetChoices();
model.set('choices', choices);
console.log("Done Getting Data");
_this.resolve();
}, 200 + Math.random() * 500);
});
};
return {
view: view,
model: model,
id: facetDef.id
};
};
var createFacet = function(facetDef){
var facet = null;
if (facetDef.type == 'list'){
facet = createListFacet(facetDef);
}
return facet;
};
var facetsStateToActionQueue= function(facetsState){
var actionQueue = {
async: false,
actions: []
};
// Process states for each facet.
_.each(facetsState.facetStates, function(facetState){
var facetActions = [];
var facetDef = facetDefinitions[facetState.id];
// Process actions for each facet.
_.each(facetState.actions, function(actionDef){
// Convert string shortcuts to objects.
if (typeof(actionDef) == 'string'){
actionDef = {action: actionDef};
}
// Process action types.
var action = null;
switch(actionDef.action){
case "create":
action = function(){
return $.Deferred(function(){
console.log("create", facetState);
var _this = this;
var facet = createFacet(facetDef);
facets[facet.id] = facet;
facet_collection_view.addFacetView(facet.view);
_this.resolve();
});
};
break;
case "getData":
action = function(){
return $.Deferred(function(){
console.log("getData", facetState);
var facet = facets[facetDef.id];
var _this = this;
$.when(facet.model.getData()).then(function(){;
_this.resolve();
});
});
};
break;
case "selectChoices":
action = function(){
return $.Deferred(function(){
console.log("selectChoices", facetState);
var facet = facets[facetDef.id];
if (actionDef.byIndices){
var choices = facet.model.get('choices');
var selected_choices = [];
_.each(actionDef.byIndices, function(idx){
var selected_choice = choices[idx];
selected_choices.push(selected_choice);
});
var selection = {};
_.each(selected_choices, function(selected_choice){
selection[selected_choice.id] = true;
});
facet.model.get('selection').set(selection);
}
this.resolve();
});
};
break;
default:
break;
}
if (action){
facetActions.push(action);
}
});
// Add facet actions to action queue.
_.each(facetActions, function(action){
actionQueue.actions.push({type: 'action', action: action});
});
});
return actionQueue;
};
var processActionQueue = function(actionQueue){
var action = function(){
var deferred = $.Deferred();
// Assemble actions from definitions.
var sub_actions = [];
_.each(actionQueue.actions, function(item){
if (item.type == 'action'){
sub_actions.push(item.action);
}
else if (item.type == 'actionSet'){
console.log('actionSet');
}
});
// If there were sub actions...
if (sub_actions.length > 0){
// Deferred representing final sub deferred.
var final_sub_deferred = null;
// If async, execute actions in parallel.
if (actionQueue.async){
console.log("async");
var sub_deferreds = [];
_.each(sub_actions, function(sub_action){
var sub_deferred = sub_action();
sub_deferreds.push(sub_deferred);
});
final_sub_deferred = $.when.apply($, sub_deferreds);
}
// Otherwise, execute actions in sequence.
else{
// Initialize w/ first subaction.
final_sub_deferred = sub_actions[0]();
// Trigger subsequent subactions in sequence.
for (var i = 1; i < sub_actions.length; i++){
var i_ = i;
final_sub_deferred = final_sub_deferred.pipe(function(){
return sub_actions[i_]();
});
}
}
// When final deferred is complete, resolve.
final_sub_deferred.done(function(){
deferred.resolve();
});
}
// If there were no sub actions, resolve immediately.
else{
deferred.resolve();
}
return deferred;
};
return action;
};
$(document).ready(function(){
console.log("document.ready");
var actionQueue = facetsStateToActionQueue(facetsState);
console.log("aq is: ", actionQueue);
var action = processActionQueue(actionQueue);
var dfd = action();
dfd.done(function(){
console.log("all done");
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment