Compare commits

...

2 Commits

Author SHA1 Message Date
Henry
92d4bf7b07 Refactoring MutableObject 2016-08-18 21:45:46 -07:00
Henry
8d245305a4 Added automatic synchronization of model 2016-08-18 10:38:16 -07:00
4 changed files with 82 additions and 78 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,13 @@ 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;
@@ -20,9 +27,19 @@ define([
};
MutableObject.prototype.on = function(path, callback) {
function synchronizeModel(value) {
if (path === '*'){
this.object = value;
} else {
_.set(object, path, value)
}
callback(value);
}
var fullPath = qualifiedEventName(this.object, path);
this.eventEmitter.on(fullPath, callback);
this.unlisteners.push(this.eventEmitter.off.bind(this.eventEmitter, fullPath, callback));
var wrappedFunction = synchronizeModel.bind(this);
this.eventEmitter.on(fullPath, wrappedFunction);
this.unlisteners.push(this.eventEmitter.off.bind(this.eventEmitter, fullPath, wrappedFunction));
};
MutableObject.prototype.set = function (path, value) {
@@ -35,9 +52,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);
@@ -34,36 +34,30 @@ define([
}
TodoView.prototype.objectChanged = function (object) {
if (this.mutableObject) {
this.mutableObject.stopListening();
}
this.mutableObject = mct.Objects.getMutable(object);
this.render();
//If anything on object changes, re-render view
this.mutableObject.on("*", this.objectChanged);
this.render(object);
};
TodoView.prototype.show = function (container) {
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.objectChanged(this.domainObject);
mct.selection.on('change', self.render);
});
mct.selection.on('change', self.render);
//If anything on object changes, re-render view
this.mutableObject.on("*", this.objectChanged);
};
TodoView.prototype.destroy = function () {
@@ -84,10 +78,10 @@ define([
}, this);
};
TodoView.prototype.render = function () {
TodoView.prototype.render = function (domainObject) {
var $els = this.$els;
var mutableObject = this.mutableObject;
var tasks = mutableObject.get('tasks');
var mutableObject = mct.Objects.getMutable(domainObject);
var tasks = domainObject.tasks;
var $message = $els.find('.example-message');
var $list = $els.find('.example-todo-task-list');
var $buttons = this.$buttons;
@@ -140,55 +134,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 () {