Skip to content

Commit 66103fc

Browse files
fix(views): Allow same views object to be reused in multiple states
Closes #3353
1 parent 20d1fcd commit 66103fc

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/statebuilders/views.ts

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ export function ng1ViewsBuilder(state: StateObject) {
4848
// Account for views: { header: "headerComponent" }
4949
if (isString(config)) config = { component: <string> config };
5050

51+
// Make a shallow copy of the config object
52+
config = extend({}, config);
53+
5154
if (hasAnyKey(compKeys, config) && hasAnyKey(nonCompKeys, config)) {
5255
throw new Error(`Cannot combine: ${compKeys.join("|")} with: ${nonCompKeys.join("|")} in stateview: '${name}@${state.name}'`);
5356
}

test/ng1StateBuilderSpec.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ describe('Ng1 StateBuilder', function() {
1515
builder.builder('views', ng1ViewsBuilder);
1616
});
1717

18-
it('should return a new views object, and copy keys from state def, if no `views` is defined in the state def', function() {
18+
it('should use the state object to build a default view, when no `views` property is found', function() {
1919
var config = { url: "/foo", templateUrl: "/foo.html", controller: "FooController", parent: parent };
2020
var built = builder.builder('views')(config);
2121

2222
expect(built.$default).not.toEqualData(config);
2323
expect(built.$default).toEqualData({ templateUrl: "/foo.html", controller: "FooController", resolveAs: '$resolve' });
2424
});
2525

26-
it("should return modified view config object if `views` is defined in the state def", function() {
26+
it('It should use the views object to build views, when defined, function() {
2727
var config = { a: { foo: "bar", controller: "FooController" } };
28-
expect(builder.builder('views')({ parent: parent, views: config })).toEqual(config);
28+
let builtViews = builder.builder('views')({ parent: parent, views: config });
29+
expect(builtViews.a.foo).toEqualData(config.a.foo);
30+
expect(builtViews.a.controller).toEqualData(config.a.controller);
2931
});
3032

3133
it("should not allow a view config with both component and template keys", inject(function($injector) {

test/viewDirectiveSpec.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import * as angular from "angular";
2-
import "./util/matchers";
3-
import { extend } from "ui-router-core";
1+
import * as angular from 'angular';
2+
import './util/matchers';
3+
import { extend } from 'ui-router-core';
44

55
declare let inject, jasmine;
66

@@ -1372,6 +1372,20 @@ describe('angular 1.5+ style .component()', function() {
13721372
expect(header.textContent).toBe('#awesome#');
13731373
expect(content.textContent).toBe('-DATA!-');
13741374
});
1375+
1376+
// Test for https://github.com/angular-ui/ui-router/issues/3353
1377+
it('should allow different states to reuse view declaration', function () {
1378+
let views = {
1379+
header: { component: 'header' },
1380+
content: { component: 'ngComponent' },
1381+
};
1382+
1383+
let stateDef1 = { name: 'def1', url: '/def1', views: views, };
1384+
let stateDef2 = { name: 'def2', url: '/def2', views: views, };
1385+
1386+
$stateProvider.state(stateDef1);
1387+
$stateProvider.state(stateDef2);
1388+
});
13751389
});
13761390
}
13771391

0 commit comments

Comments
 (0)