cloak.controller
Version:
Cloak.js controller module
95 lines (81 loc) • 1.92 kB
JavaScript
var Class = require('cloak.core/utils/class');
var Promise = require('cloak.core/utils/promise');
var merge = require('cloak.core/utils/merge');
var isThenable = require('cloak.core/node_modules/promise-es6/lib/utils').thenable;
var Controller = module.exports = Class.extend({
//
// Initialize
//
init: function() {
if (typeof this.initialize === 'function') {
this.initialize.apply(this, arguments);
}
},
//
// Default initialize method
//
initialize: function(elem) {
this.elem = elem;
},
//
// Runs the `predraw > load > draw` process
//
// @return promise
//
run: function() {
var self = this;
try {
var predraw = this.predraw();
var load = this.load();
} catch(err) {
return Promise.reject(err);
}
if (! isThenable(predraw)) {
predraw = Promise.resolve();
}
if (! isThenable(load)) {
load = Promise.resolve(load);
}
return Promise.all([ load, predraw ])
.then(function(data) {
return self.draw(data[0]);
});
},
//
// The first drawing step, useful to do things like drawing loading spinners
// while still fetching content; May return a promise if async
//
// @return promise
//
predraw: function() {
//
},
//
// The second step, runs parallel to any `predraw` method. Used to load in any
// data needed for full rendering; May return a promise if async
//
// @return promise
//
load: function() {
//
},
//
// The final drawing step, runs after `predraw` and `load`, draws the actual
// functioning content; May return a promise if async
//
// @param {data} any data resulting from the `load` method
// @return promise
//
draw: function(data) {
//
},
//
// Called to gracefully undraw the view, can be used for things like exit animations
// or resource cleanup; May return a promise if async
//
// @return promise
//
undraw: function() {
//
}
});