Skip to content

Commit d37caa7

Browse files
authored
Merge pull request nasa#1370 from nasa/open1367
[Edit] Set key on represent
2 parents c4d47dd + a768b12 commit d37caa7

File tree

3 files changed

+124
-42
lines changed

3 files changed

+124
-42
lines changed

platform/commonUI/edit/bundle.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,6 @@ define([
374374
{
375375
"implementation": EditRepresenter,
376376
"depends": [
377-
"$q",
378377
"$log"
379378
]
380379
},

platform/commonUI/edit/src/representers/EditRepresenter.js

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -43,57 +43,51 @@ define(
4343
* @implements {Representer}
4444
* @constructor
4545
*/
46-
function EditRepresenter($q, $log, scope) {
47-
var self = this;
46+
function EditRepresenter($log, $scope) {
47+
this.$log = $log;
48+
this.$scope = $scope;
4849

49-
this.scope = scope;
50-
51-
// Mutate and persist a new version of a domain object's model.
52-
function doMutate(model) {
53-
var domainObject = self.domainObject;
54-
55-
// First, mutate; then, persist.
56-
return $q.when(domainObject.useCapability("mutation", function () {
57-
return model;
58-
}));
59-
}
50+
this.$scope.commit = this.commit.bind(this);
51+
}
6052

61-
// Handle changes to model and/or view configuration
62-
function commit(message) {
63-
// Look up from scope; these will have been populated by
64-
// mct-representation.
65-
var model = scope.model,
66-
configuration = scope.configuration,
67-
domainObject = self.domainObject;
53+
/**
54+
* Commit any changes made to the in-scope model to the domain object.
55+
* Also commits any changes made to $scope.configuration to the proper
56+
* configuration value for the current representation.
57+
*
58+
* @param {String} message a message to log with the commit message.
59+
*/
60+
EditRepresenter.prototype.commit = function (message) {
61+
var model = this.$scope.model,
62+
configuration = this.$scope.configuration,
63+
domainObject = this.domainObject;
6864

69-
// Log the commit message
70-
$log.debug([
71-
"Committing ",
72-
domainObject && domainObject.getModel().name,
73-
"(" + (domainObject && domainObject.getId()) + "):",
74-
message
75-
].join(" "));
65+
this.$log.debug([
66+
"Committing ",
67+
domainObject && domainObject.getModel().name,
68+
"(" + (domainObject && domainObject.getId()) + "):",
69+
message
70+
].join(" "));
7671

77-
// Update the configuration stored in the model, and persist.
78-
if (domainObject) {
79-
// Configurations for specific views are stored by
80-
// key in the "configuration" field of the model.
81-
if (self.key && configuration) {
82-
model.configuration = model.configuration || {};
83-
model.configuration[self.key] = configuration;
84-
}
85-
doMutate(model);
72+
if (this.domainObject) {
73+
if (this.key && configuration) {
74+
model.configuration = model.configuration || {};
75+
model.configuration[this.key] = configuration;
8676
}
77+
domainObject.useCapability('mutation', function () {
78+
return model;
79+
});
8780
}
88-
89-
// Place the "commit" method in the scope
90-
scope.commit = commit;
91-
92-
}
81+
};
9382

9483
// Handle a specific representation of a specific domain object
9584
EditRepresenter.prototype.represent = function (representation, representedObject) {
9685
this.domainObject = representedObject;
86+
if (representation) {
87+
this.key = representation.key;
88+
} else {
89+
delete this.key;
90+
}
9791
};
9892

9993
// Respond to the destruction of the current representation.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*****************************************************************************
2+
* Open MCT, Copyright (c) 2014-2016, United States Government
3+
* as represented by the Administrator of the National Aeronautics and Space
4+
* Administration. All rights reserved.
5+
*
6+
* Open MCT is licensed under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0.
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations
15+
* under the License.
16+
*
17+
* Open MCT includes source code licensed under additional open source
18+
* licenses. See the Open Source Licenses file (LICENSES.md) included with
19+
* this source code distribution or the Licensing information page available
20+
* at runtime from the About dialog for additional information.
21+
*****************************************************************************/
22+
23+
define([
24+
'../../src/representers/EditRepresenter'
25+
], function (
26+
EditRepresenter
27+
) {
28+
describe('EditRepresenter', function () {
29+
var $log,
30+
$scope,
31+
representer;
32+
33+
34+
beforeEach(function () {
35+
$log = jasmine.createSpyObj('$log', ['debug']);
36+
$scope = {};
37+
representer = new EditRepresenter($log, $scope);
38+
});
39+
40+
it('injects a commit function in scope', function () {
41+
expect($scope.commit).toEqual(jasmine.any(Function));
42+
});
43+
44+
describe('representation', function () {
45+
var domainObject,
46+
representation;
47+
48+
beforeEach(function () {
49+
domainObject = jasmine.createSpyObj('domainObject', [
50+
'getId',
51+
'getModel',
52+
'useCapability'
53+
]);
54+
55+
domainObject.getId.andReturn('anId');
56+
domainObject.getModel.andReturn({name: 'anObject'});
57+
58+
representation = {
59+
key: 'someRepresentation'
60+
};
61+
$scope.model = {name: 'anotherName'};
62+
$scope.configuration = {some: 'config'};
63+
representer.represent(representation, domainObject);
64+
});
65+
66+
it('logs a message when commiting', function () {
67+
$scope.commit('Test Message');
68+
expect($log.debug)
69+
.toHaveBeenCalledWith('Committing anObject (anId): Test Message');
70+
});
71+
72+
it('mutates the object when committing', function () {
73+
$scope.commit('Test Message');
74+
75+
expect(domainObject.useCapability)
76+
.toHaveBeenCalledWith('mutation', jasmine.any(Function));
77+
78+
var mutateValue = domainObject.useCapability.calls[0].args[1]();
79+
80+
expect(mutateValue.configuration.someRepresentation)
81+
.toEqual({some: 'config'});
82+
expect(mutateValue.name).toEqual('anotherName');
83+
});
84+
85+
});
86+
87+
88+
});
89+
});

0 commit comments

Comments
 (0)