Skip to content

Commit 01dee3c

Browse files
Portugal, Marcelomportuga
Portugal, Marcelo
authored andcommitted
fix(importer): Fix console error on opening grid menu.
Updated the ui-grid-importer-menu-item directive to optionally require the uiGrid controller, much like the regular ui-grid-menu-item directive. fix #6535
1 parent c461cdc commit 01dee3c

File tree

3 files changed

+95
-11
lines changed

3 files changed

+95
-11
lines changed

misc/tutorial/207_importing_data.ngdoc

+8-6
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,26 @@ illustrates that doing so is not mandatory).
5959
<file name="app.js">
6060
var app = angular.module('app', ['ngAnimate', 'ngTouch', 'ui.grid', 'ui.grid.importer']);
6161

62-
app.controller('MainCtrl', ['$scope', '$http', '$interval', function ($scope, $http, $interval) {
62+
app.controller('MainCtrl', function ($scope, $http, $interval) {
63+
var vm = this;
64+
6365
$scope.data = [];
64-
$scope.gridOptions = {
66+
vm.gridOptions = {
6567
enableGridMenu: true,
6668
data: 'data',
6769
importerDataAddCallback: function ( grid, newObjects ) {
6870
$scope.data = $scope.data.concat( newObjects );
6971
},
7072
onRegisterApi: function(gridApi){
71-
$scope.gridApi = gridApi;
73+
vm.gridApi = gridApi;
7274
}
7375
};
74-
}]);
76+
});
7577
</file>
7678

7779
<file name="index.html">
78-
<div ng-controller="MainCtrl">
79-
<div ui-grid="gridOptions" ui-grid-importer class="grid"></div>
80+
<div ng-controller="MainCtrl as $ctrl">
81+
<div ui-grid="$ctrl.gridOptions" ui-grid-importer class="grid"></div>
8082
</div>
8183
</file>
8284

src/features/importer/js/importer.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -722,28 +722,42 @@
722722
return {
723723
replace: true,
724724
priority: 0,
725-
require: '^uiGrid',
725+
require: '?^uiGrid',
726726
scope: false,
727727
templateUrl: 'ui-grid/importerMenuItem',
728728
link: function ($scope, $elm, $attrs, uiGridCtrl) {
729+
var grid;
730+
729731
var handleFileSelect = function( event ){
730732
var target = event.srcElement || event.target;
731733

732734
if (target && target.files && target.files.length === 1) {
733735
var fileObject = target.files[0];
734-
uiGridImporterService.importThisFile( grid, fileObject );
735-
target.form.reset();
736+
737+
// Define grid if the uiGrid controller is present
738+
if (typeof(uiGridCtrl) !== 'undefined' && uiGridCtrl) {
739+
grid = uiGridCtrl.grid;
740+
741+
uiGridImporterService.importThisFile( grid, fileObject );
742+
target.form.reset();
743+
} else {
744+
gridUtil.logError('Could not import file because UI Grid was not found.');
745+
}
736746
}
737747
};
738748

739749
var fileChooser = $elm[0].querySelectorAll('.ui-grid-importer-file-chooser');
740-
var grid = uiGridCtrl.grid;
741750

742751
if ( fileChooser.length !== 1 ){
743752
gridUtil.logError('Found > 1 or < 1 file choosers within the menu item, error, cannot continue');
744753
} else {
745-
fileChooser[0].addEventListener('change', handleFileSelect, false); // TODO: why the false on the end? Google
754+
fileChooser[0].addEventListener('change', handleFileSelect, false);
746755
}
756+
757+
$scope.$on('$destroy', function unbindEvents() {
758+
// unbind jquery events to prevent memory leaks
759+
fileChooser[0].removeEventListener('change', handleFileSelect, false);
760+
});
747761
}
748762
};
749763
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
describe('ui.grid.importer uiGridImporterMenuItem', function() {
2+
'use strict';
3+
4+
var $compile, $templateCache, $rootScope, $scope, gridUtil, uiGridImporterService,
5+
fileChooser, uiGridImporterMenuItem;
6+
7+
function compileImporterMenuItem(elem) {
8+
uiGridImporterMenuItem = angular.element(elem);
9+
$compile(uiGridImporterMenuItem)($scope);
10+
$scope.$apply();
11+
12+
fileChooser = uiGridImporterMenuItem[0].querySelectorAll('.ui-grid-importer-file-chooser');
13+
}
14+
15+
beforeEach(function() {
16+
module('ui.grid');
17+
module('ui.grid.importer');
18+
19+
inject(function(_$compile_, _$rootScope_, _$templateCache_, _gridUtil_, _uiGridImporterService_) {
20+
$compile = _$compile_;
21+
$rootScope = _$rootScope_;
22+
$templateCache = _$templateCache_;
23+
gridUtil = _gridUtil_;
24+
uiGridImporterService = _uiGridImporterService_;
25+
});
26+
27+
spyOn(gridUtil, 'logError').and.callFake(angular.noop);
28+
spyOn(uiGridImporterService, 'importThisFile').and.callFake(angular.noop);
29+
30+
$scope = $rootScope.$new();
31+
32+
compileImporterMenuItem('<div ui-grid-importer-menu-item></div>');
33+
});
34+
afterEach(function() {
35+
gridUtil.logError.calls.reset();
36+
uiGridImporterService.importThisFile.calls.reset();
37+
});
38+
it('should compile the menu item', function() {
39+
expect(uiGridImporterMenuItem.hasClass('ui-grid-menu-item')).toBe(true);
40+
});
41+
it('should do nothing when a change event is fired and there are no files selected', function() {
42+
var event = new Event('change');
43+
44+
fileChooser[0].dispatchEvent(event);
45+
46+
expect(gridUtil.logError).not.toHaveBeenCalled();
47+
expect(uiGridImporterService.importThisFile).not.toHaveBeenCalled();
48+
});
49+
it('should log an error if more than one file choosers are present on the menu item', function() {
50+
$templateCache.put('ui-grid/importerMenuItem', '<div class="ui-grid-menu-item"><span class="ui-grid-importer-file-chooser"></span>' +
51+
'<span class="ui-grid-importer-file-chooser"></span></div>');
52+
compileImporterMenuItem('<div ui-grid-importer-menu-item></div>');
53+
54+
expect(gridUtil.logError).toHaveBeenCalledWith('Found > 1 or < 1 file choosers within the menu item, error, cannot continue');
55+
});
56+
describe('on $destroy', function() {
57+
beforeEach(function() {
58+
spyOn(fileChooser[0], 'removeEventListener').and.callThrough();
59+
$scope.$broadcast('$destroy');
60+
});
61+
afterEach(function() {
62+
fileChooser[0].removeEventListener.calls.reset();
63+
});
64+
it('should remove all event handlers', function() {
65+
expect(fileChooser[0].removeEventListener).toHaveBeenCalled();
66+
});
67+
});
68+
});

0 commit comments

Comments
 (0)