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">
require(['main'], function (mct) {
require([
'./tutorials/todo/todo',
'./example/imagery/bundle',
'./example/eventGenerator/bundle',
'./example/generator/bundle',
], function () {
'./example/generator/bundle'
], function (TodoPlugin) {
mct.install(TodoPlugin);
mct.run();
})
});

View File

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

View File

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

View File

@ -26,7 +26,7 @@ define([
function TodoView(domainObject) {
this.domainObject = domainObject;
this.mutableObject = undefined;
this.mutableObject = mct.Objects.getMutable(domainObject);
this.filterValue = "all";
this.render = this.render.bind(this);
@ -37,6 +37,8 @@ define([
if (this.mutableObject) {
this.mutableObject.stopListening();
}
//Swap out local object instance for fresh one
this.domainObject = object;
this.mutableObject = mct.Objects.getMutable(object);
this.render();
@ -48,22 +50,20 @@ define([
var self = this;
this.destroy();
mct.Objects.get(utils.parseKeyString(self.domainObject.getId())).then(function (object) {
self.$els = $(todoTemplate);
self.$buttons = {
all: self.$els.find('.example-todo-button-all'),
incomplete: self.$els.find('.example-todo-button-incomplete'),
complete: self.$els.find('.example-todo-button-complete')
};
self.$els = $(todoTemplate);
self.$buttons = {
all: self.$els.find('.example-todo-button-all'),
incomplete: self.$els.find('.example-todo-button-incomplete'),
complete: self.$els.find('.example-todo-button-complete')
};
$(container).empty().append(self.$els);
$(container).empty().append(self.$els);
self.initialize();
self.objectChanged(object);
self.initialize();
self.render();
mct.selection.on('change', self.render);
});
mct.selection.on('change', self.render);
};
TodoView.prototype.destroy = function () {
@ -82,12 +82,15 @@ define([
Object.keys(this.$buttons).forEach(function (k) {
this.$buttons[k].on('click', this.setFilter.bind(this, k));
}, this);
//If anything on object changes, re-render view
this.mutableObject.on("*", this.objectChanged);
};
TodoView.prototype.render = function () {
var $els = this.$els;
var mutableObject = this.mutableObject;
var tasks = mutableObject.get('tasks');
var mutableObject = mct.Objects.getMutable(this.domainObject);
var tasks = this.domainObject.tasks;
var $message = $els.find('.example-message');
var $list = $els.find('.example-todo-task-list');
var $buttons = this.$buttons;
@ -140,55 +143,50 @@ define([
function TodoToolbarView(domainObject) {
this.domainObject = domainObject;
this.mutableObject = undefined;
this.handleSelectionChange = this.handleSelectionChange.bind(this);
this.mutableObject = mct.Objects.getMutable(domainObject);
}
TodoToolbarView.prototype.show = function (container) {
var self = this;
this.destroy();
mct.Objects.get(utils.parseKeyString(this.domainObject.getId())).then(function (wrappedObject){
var $els = $(toolbarTemplate);
var $add = $els.find('a.example-add');
var $remove = $els.find('a.example-remove');
self.mutableObject = mct.Objects.getMutable(wrappedObject);
$(container).append($els);
var $els = $(toolbarTemplate);
var $add = $els.find('a.example-add');
var $remove = $els.find('a.example-remove');
$add.on('click', function () {
var $dialog = $(dialogTemplate),
view = {
show: function (container) {
$(container).append($dialog);
},
destroy: function () {}
};
$(container).append($els);
$add.on('click', function () {
var $dialog = $(dialogTemplate),
view = {
show: function (container) {
$(container).append($dialog);
},
destroy: function () {}
};
mct.dialog(view, "Add a Task").then(function () {
var description = $dialog.find('input').val();
var tasks = self.mutableObject.get('tasks');
tasks.push({ description: description });
self.mutableObject.set('tasks', tasks);
});
mct.dialog(view, "Add a Task").then(function () {
var description = $dialog.find('input').val();
var tasks = self.domainObject.tasks;
tasks.push({ description: description });
self.mutableObject.set('tasks', tasks);
});
$remove.on('click', function () {
var index = mct.selection.selected()[0].index;
if (index !== undefined) {
var tasks = self.mutableObject.get('tasks').filter(function (t, i) {
return i !== index;
});
self.mutableObject.set("tasks", tasks);
self.mutableObject.set("selected", undefined);
mct.selection.clear();
}
});
self.$remove = $remove;
self.handleSelectionChange();
mct.selection.on('change', self.handleSelectionChange);
});
$remove.on('click', function () {
var index = mct.selection.selected()[0].index;
if (index !== undefined) {
var tasks = self.mutableObject.tasks.filter(function (t, i) {
return i !== index;
});
self.mutableObject.set("tasks", tasks);
self.mutableObject.set("selected", undefined);
mct.selection.clear();
}
});
self.$remove = $remove;
self.handleSelectionChange();
mct.selection.on('change', self.handleSelectionChange);
};
TodoToolbarView.prototype.handleSelectionChange = function () {