[API] Update view API

This commit is contained in:
Victor Woeltjen
2016-06-17 11:16:08 -07:00
parent 2240a87ddc
commit 96316de6e4
3 changed files with 37 additions and 33 deletions

View File

@ -21,9 +21,7 @@ define(['angular'], function (angular) {
} }
var view = factories[key](mctObject); var view = factories[key](mctObject);
var elements = view.elements(); view.show(element[0]);
element.empty();
element.append(elements);
} }
function setKey(k) { function setKey(k) {

View File

@ -1,21 +1,24 @@
define(['EventEmitter'], function (EventEmitter) { define(function () {
function View() { function View() {
EventEmitter.call(this);
} }
View.prototype = Object.create(EventEmitter.prototype); /**
* Show this view in the specified container. If this view is already
* showing elsewhere, it will be removed from that location.
*
* @param {HTMLElement} container the element to populate
*/
View.prototype.show = function (container) {
};
['elements', 'model'].forEach(function (method) { /**
View.prototype[method] = function (value) { * Release any resources associated with this view.
this.viewState = *
this.viewState || { elements: [], model: undefined }; * Subclasses should override this method to release any resources
if (arguments.length > 0) { * they obtained during a `show` call.
this.viewState[method] = value; */
this.emit(method, value); View.prototype.destroy = function () {
} };
return this.viewState[method];
}
});
return View; return View;
}); });

View File

@ -22,23 +22,26 @@ define([
}); });
function TodoView(domainObject) { function TodoView(domainObject) {
mct.View.apply(this); this.domainObject = domainObject;
this.filterValue = "all"; this.filterValue = "all";
this.elements($(todoTemplate));
var $els = $(this.elements());
this.$buttons = {
all: $els.find('.example-todo-button-all'),
incomplete: $els.find('.example-todo-button-incomplete'),
complete: $els.find('.example-todo-button-complete')
};
this.initialize();
this.on('model', this.render.bind(this));
this.model(domainObject);
} }
TodoView.prototype = Object.create(mct.View.prototype); TodoView.prototype.show = function (container) {
this.$els = $(todoTemplate);
this.$buttons = {
all: this.$els.find('.example-todo-button-all'),
incomplete: this.$els.find('.example-todo-button-incomplete'),
complete: this.$els.find('.example-todo-button-complete')
};
$(container).empty().append(this.$els);
this.initialize();
this.render();
};
TodoView.prototype.destroy = function () {
};
TodoView.prototype.setFilter = function (value) { TodoView.prototype.setFilter = function (value) {
this.filterValue = value; this.filterValue = value;
@ -52,8 +55,8 @@ define([
}; };
TodoView.prototype.render = function () { TodoView.prototype.render = function () {
var $els = $(this.elements()); var $els = this.$els;
var domainObject = this.model(); var domainObject = this.domainObject;
var tasks = domainObject.getModel().tasks; var tasks = domainObject.getModel().tasks;
var $message = $els.find('.example-message'); var $message = $els.find('.example-message');
var $list = $els.find('.example-todo-task-list'); var $list = $els.find('.example-todo-task-list');