refactored sections
This commit is contained in:
89
front-ui/app/stores/sectionStore.js
Normal file
89
front-ui/app/stores/sectionStore.js
Normal file
@@ -0,0 +1,89 @@
|
||||
var AppDispatcher = require('../dispatcher/appDispatcher');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var SectionCollection = require('../models/sectionCollection');
|
||||
var SectionConstants = require('../constants/sectionConstants');
|
||||
var _ = require('underscore');
|
||||
|
||||
|
||||
var sectionState = {
|
||||
sections : [],
|
||||
hoveredSection : ''
|
||||
};
|
||||
|
||||
|
||||
var loadSections = function() {
|
||||
var sections = new SectionCollection();
|
||||
sections.fetch({success: function() {
|
||||
sectionState.sections = sections.models;
|
||||
// change will be called automatically when
|
||||
// action is run but we need to emit it again
|
||||
// when the data arive
|
||||
// it's a bit "unfluxy" but convenient.
|
||||
// "true philosophy" would be to run another "data arrived" action
|
||||
SectionStore.emitChange();
|
||||
}});
|
||||
};
|
||||
|
||||
var setHovered = function(id) {
|
||||
sectionState.hoveredSection = id;
|
||||
}
|
||||
|
||||
|
||||
// Extend SectionStore with EventEmitter to add eventing capabilities
|
||||
var SectionStore = _.extend({}, EventEmitter.prototype, {
|
||||
|
||||
// Return Single Item With Details
|
||||
getState: function() {
|
||||
return sectionState;
|
||||
},
|
||||
|
||||
// Emit Change event
|
||||
emitChange: function() {
|
||||
console.log("Emmiting Section change!");
|
||||
this.emit('change');
|
||||
},
|
||||
|
||||
// Add change listener
|
||||
addChangeListener: function(callback) {
|
||||
this.on('change', callback);
|
||||
},
|
||||
|
||||
// Remove change listener
|
||||
removeChangeListener: function(callback) {
|
||||
this.removeListener('change', callback);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
// Register callback with AppDispatcher
|
||||
AppDispatcher.register(function(payload) {
|
||||
var action = payload.action;
|
||||
var text;
|
||||
|
||||
switch(action.actionType) {
|
||||
|
||||
// Respond to SELECT_ITEM action
|
||||
case SectionConstants.LOAD_SECTIONS:
|
||||
loadSections();
|
||||
break;
|
||||
|
||||
case SectionConstants.SET_SECTION_HOVER:
|
||||
setHovered(action.section.get('id'));
|
||||
break;
|
||||
|
||||
case SectionConstants.UNSET_SECTION_HOVER:
|
||||
setHovered('');
|
||||
break;
|
||||
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
|
||||
// If action was responded to, emit change event
|
||||
SectionStore.emitChange();
|
||||
return true;
|
||||
|
||||
});
|
||||
|
||||
module.exports = SectionStore;
|
||||
Reference in New Issue
Block a user