var CourseActions, CourseListingActions, CourseListingStore, CourseStore, DELETED, DELETING, FAILED, LOADED, LOADING, _, flux, ref;
_ = require('underscore');
flux = require('flux-react');
ref = require('./course'), CourseActions = ref.CourseActions, CourseStore = ref.CourseStore;
LOADING = 'loading';
LOADED = 'loaded';
FAILED = 'failed';
DELETING = 'deleting';
DELETED = 'deleted';
CourseListingActions = flux.createActions(['load', 'loaded', 'reset', 'FAILED', 'delete', 'deleted']);
CourseListingStore = flux.createStore({
actions: _.values(CourseListingActions),
_asyncStatus: null,
load: function() {
this._asyncStatus = LOADING;
return this.emit('load');
},
reset: function() {
this._course_ids = [];
CourseActions.reset();
this._asyncStatus = null;
return this.emitChange();
},
FAILED: function() {
this._asyncStatus = FAILED;
return this.emit('failed');
},
loaded: function(courses) {
this._course_ids = _.map(courses, function(course) {
CourseActions.loaded(course, course.id);
return course.id;
});
this._asyncStatus = LOADED;
return this.emit('loaded');
},
"delete": function(courseId) {
this._asyncStatus[courseId] = DELETING;
this._course_ids = _.without(this._course_ids, courseId);
return this.emit(DELETING);
},
deleted: function(courseId) {
this._asyncStatus[courseId] = DELETED;
return this.emit(DELETED);
},
exports: {
isLoading: function() {
return this._asyncStatus === LOADING;
},
isLoaded: function() {
return this._asyncStatus === LOADED;
},
isFailed: function() {
return this._asyncStatus === FAILED;
},
ensureLoaded: function() {
if (CourseListingStore.isLoaded()) {
return false;
} else {
if (!CourseListingStore.isLoading()) {
CourseListingActions.load();
}
return true;
}
},
allCourses: function() {
return _.compact(_.map(this._course_ids, CourseStore.get));
}
}
});
module.exports = {
CourseListingActions: CourseListingActions,
CourseListingStore: CourseListingStore
};
|