Removed setters from MutableObject and fixed non-working todo list tutorial

Refactoring MutableObject

Fixed non-working todo example
This commit is contained in:
Henry
2016-08-18 10:38:16 -07:00
committed by Andrew Henry
parent 1147f3aa47
commit 9c88b7ce1d
4 changed files with 71 additions and 67 deletions

View File

@ -31,10 +31,12 @@
<script type="text/javascript"> <script type="text/javascript">
require(['main'], function (mct) { require(['main'], function (mct) {
require([ require([
'./tutorials/todo/todo',
'./example/imagery/bundle', './example/imagery/bundle',
'./example/eventGenerator/bundle', './example/eventGenerator/bundle',
'./example/generator/bundle', './example/generator/bundle'
], function () { ], function (TodoPlugin) {
mct.install(TodoPlugin);
mct.run(); mct.run();
}) })
}); });

View File

@ -3,6 +3,14 @@ define([
], function ( ], function (
_ _
) { ) {
/**
* The MutableObject wraps a DomainObject and provides getters and
* setters for
* @param eventEmitter
* @param object
* @constructor
*/
function MutableObject(eventEmitter, object) { function MutableObject(eventEmitter, object) {
this.eventEmitter = eventEmitter; this.eventEmitter = eventEmitter;
this.object = object; this.object = object;
@ -35,9 +43,5 @@ define([
this.eventEmitter.emit(qualifiedEventName(this.object, '*'), this.object); this.eventEmitter.emit(qualifiedEventName(this.object, '*'), this.object);
}; };
MutableObject.prototype.get = function (path) {
return _.get(this.object, path);
};
return MutableObject; return MutableObject;
}); });

View File

@ -60,25 +60,25 @@ define(['./MutableObject'], function (MutableObject) {
}); });
it('Supports getting and setting of object properties', function () { it('Supports getting and setting of object properties', function () {
expect(mutableObject.get('stringProperty')).toEqual('stringValue'); expect(domainObject.stringProperty).toEqual('stringValue');
mutableObject.set('stringProperty', 'updated'); mutableObject.set('stringProperty', 'updated');
expect(mutableObject.get('stringProperty')).toEqual('updated'); expect(domainObject.stringProperty).toEqual('updated');
var newArrayProperty = []; var newArrayProperty = [];
expect(mutableObject.get('arrayProperty')).toEqual(arrayProperty); expect(domainObject.arrayProperty).toEqual(arrayProperty);
mutableObject.set('arrayProperty', newArrayProperty); mutableObject.set('arrayProperty', newArrayProperty);
expect(mutableObject.get('arrayProperty')).toEqual(newArrayProperty); expect(domainObject.arrayProperty).toEqual(newArrayProperty);
var newObjectProperty = []; var newObjectProperty = [];
expect(mutableObject.get('objectProperty')).toEqual(objectProperty); expect(domainObject.objectProperty).toEqual(objectProperty);
mutableObject.set('objectProperty', newObjectProperty); mutableObject.set('objectProperty', newObjectProperty);
expect(mutableObject.get('objectProperty')).toEqual(newObjectProperty); expect(domainObject.objectProperty).toEqual(newObjectProperty);
}); });
it('Supports getting and setting of nested properties', function () { it('Supports getting and setting of nested properties', function () {
expect(mutableObject.get('objectProperty')).toEqual(objectProperty); expect(domainObject.objectProperty).toEqual(objectProperty);
expect(mutableObject.get('objectProperty.prop1')).toEqual(objectProperty.prop1); expect(domainObject.objectProperty.prop1).toEqual(objectProperty.prop1);
expect(mutableObject.get('objectProperty.prop3.propA')).toEqual(objectProperty.prop3.propA); expect(domainObject.objectProperty.prop3.propA).toEqual(objectProperty.prop3.propA);
mutableObject.set('objectProperty.prop1', 'new-prop-1'); mutableObject.set('objectProperty.prop1', 'new-prop-1');
expect(domainObject.objectProperty.prop1).toEqual('new-prop-1'); expect(domainObject.objectProperty.prop1).toEqual('new-prop-1');

View File

@ -26,7 +26,7 @@ define([
function TodoView(domainObject) { function TodoView(domainObject) {
this.domainObject = domainObject; this.domainObject = domainObject;
this.mutableObject = undefined; this.mutableObject = mct.Objects.getMutable(domainObject);
this.filterValue = "all"; this.filterValue = "all";
this.render = this.render.bind(this); this.render = this.render.bind(this);
@ -37,6 +37,8 @@ define([
if (this.mutableObject) { if (this.mutableObject) {
this.mutableObject.stopListening(); this.mutableObject.stopListening();
} }
//Swap out local object instance for fresh one
this.domainObject = object;
this.mutableObject = mct.Objects.getMutable(object); this.mutableObject = mct.Objects.getMutable(object);
this.render(); this.render();
@ -48,7 +50,6 @@ define([
var self = this; var self = this;
this.destroy(); this.destroy();
mct.Objects.get(utils.parseKeyString(self.domainObject.getId())).then(function (object) {
self.$els = $(todoTemplate); self.$els = $(todoTemplate);
self.$buttons = { self.$buttons = {
all: self.$els.find('.example-todo-button-all'), all: self.$els.find('.example-todo-button-all'),
@ -60,10 +61,9 @@ define([
self.initialize(); self.initialize();
self.objectChanged(object); self.render();
mct.selection.on('change', self.render); mct.selection.on('change', self.render);
});
}; };
TodoView.prototype.destroy = function () { TodoView.prototype.destroy = function () {
@ -82,12 +82,15 @@ define([
Object.keys(this.$buttons).forEach(function (k) { Object.keys(this.$buttons).forEach(function (k) {
this.$buttons[k].on('click', this.setFilter.bind(this, k)); this.$buttons[k].on('click', this.setFilter.bind(this, k));
}, this); }, this);
//If anything on object changes, re-render view
this.mutableObject.on("*", this.objectChanged);
}; };
TodoView.prototype.render = function () { TodoView.prototype.render = function () {
var $els = this.$els; var $els = this.$els;
var mutableObject = this.mutableObject; var mutableObject = mct.Objects.getMutable(this.domainObject);
var tasks = mutableObject.get('tasks'); var tasks = this.domainObject.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');
var $buttons = this.$buttons; var $buttons = this.$buttons;
@ -140,18 +143,14 @@ define([
function TodoToolbarView(domainObject) { function TodoToolbarView(domainObject) {
this.domainObject = domainObject; this.domainObject = domainObject;
this.mutableObject = undefined;
this.handleSelectionChange = this.handleSelectionChange.bind(this); this.handleSelectionChange = this.handleSelectionChange.bind(this);
this.mutableObject = mct.Objects.getMutable(domainObject);
} }
TodoToolbarView.prototype.show = function (container) { TodoToolbarView.prototype.show = function (container) {
var self = this; var self = this;
this.destroy(); this.destroy();
mct.Objects.get(utils.parseKeyString(this.domainObject.getId())).then(function (wrappedObject){
self.mutableObject = mct.Objects.getMutable(wrappedObject);
var $els = $(toolbarTemplate); var $els = $(toolbarTemplate);
var $add = $els.find('a.example-add'); var $add = $els.find('a.example-add');
var $remove = $els.find('a.example-remove'); var $remove = $els.find('a.example-remove');
@ -169,7 +168,7 @@ define([
mct.dialog(view, "Add a Task").then(function () { mct.dialog(view, "Add a Task").then(function () {
var description = $dialog.find('input').val(); var description = $dialog.find('input').val();
var tasks = self.mutableObject.get('tasks'); var tasks = self.domainObject.tasks;
tasks.push({ description: description }); tasks.push({ description: description });
self.mutableObject.set('tasks', tasks); self.mutableObject.set('tasks', tasks);
}); });
@ -177,7 +176,7 @@ define([
$remove.on('click', function () { $remove.on('click', function () {
var index = mct.selection.selected()[0].index; var index = mct.selection.selected()[0].index;
if (index !== undefined) { if (index !== undefined) {
var tasks = self.mutableObject.get('tasks').filter(function (t, i) { var tasks = self.mutableObject.tasks.filter(function (t, i) {
return i !== index; return i !== index;
}); });
self.mutableObject.set("tasks", tasks); self.mutableObject.set("tasks", tasks);
@ -188,7 +187,6 @@ define([
self.$remove = $remove; self.$remove = $remove;
self.handleSelectionChange(); self.handleSelectionChange();
mct.selection.on('change', self.handleSelectionChange); mct.selection.on('change', self.handleSelectionChange);
});
}; };
TodoToolbarView.prototype.handleSelectionChange = function () { TodoToolbarView.prototype.handleSelectionChange = function () {