Skip to content

Commit c3d30bb

Browse files
committed
attribute injection optimization
1 parent ff55304 commit c3d30bb

File tree

4 files changed

+44
-63
lines changed

4 files changed

+44
-63
lines changed

dist/ui-scroll-grid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* angular-ui-scroll
33
* https://github.com/angular-ui/ui-scroll.git
4-
* Version: 1.4.1 -- 2016-06-02T19:39:46.238Z
4+
* Version: 1.5.0 -- 2016-06-09T21:24:24.010Z
55
* License: MIT
66
*/
77

dist/ui-scroll-jqlite.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* angular-ui-scroll
33
* https://github.com/angular-ui/ui-scroll.git
4-
* Version: 1.4.1 -- 2016-06-02T19:39:46.238Z
4+
* Version: 1.5.0 -- 2016-06-09T21:24:24.010Z
55
* License: MIT
66
*/
77

dist/ui-scroll.js

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* angular-ui-scroll
33
* https://github.com/angular-ui/ui-scroll.git
4-
* Version: 1.4.1 -- 2016-06-02T19:39:46.238Z
4+
* Version: 1.5.0 -- 2016-06-09T21:24:24.010Z
55
* License: MIT
66
*/
77

@@ -420,12 +420,15 @@ angular.module('ui.scroll', []).directive('uiScrollViewport', function () {
420420
}
421421

422422
function Adapter($attr, viewport, buffer, adjustBuffer) {
423-
var _this = this;
424-
425423
var viewportScope = viewport.scope() || $rootScope;
426424
var disabled = false;
425+
var self = this;
427426

428-
injectValue($attr.adapter, this);
427+
createValueInjector('adapter')(self);
428+
var topVisibleInjector = createValueInjector('topVisible');
429+
var topVisibleElementInjector = createValueInjector('topVisibleElement');
430+
var topVisibleScopeInjector = createValueInjector('topVisibleScope');
431+
var isLoadingInjector = createValueInjector('isLoading');
429432

430433
// Adapter API definition
431434

@@ -475,8 +478,7 @@ angular.module('ui.scroll', []).directive('uiScrollViewport', function () {
475478
};
476479

477480
this.loading = function (value) {
478-
_this.isLoading = value;
479-
injectValue($attr.isLoading, value);
481+
isLoadingInjector(value);
480482
};
481483

482484
this.calculateProperties = function () {
@@ -498,12 +500,9 @@ angular.module('ui.scroll', []).directive('uiScrollViewport', function () {
498500
topHeight += itemHeight;
499501
} else {
500502
if (isNewRow) {
501-
_this.topVisible = item.item;
502-
_this.topVisibleElement = item.element;
503-
_this.topVisibleScope = item.scope;
504-
injectValue($attr.topVisible, item.item);
505-
injectValue($attr.topVisibleElement, item.element);
506-
injectValue($attr.topVisibleScope, item.scope);
503+
topVisibleInjector(item.item);
504+
topVisibleElementInjector(item.element);
505+
topVisibleScopeInjector(item.scope);
507506
}
508507
break;
509508
}
@@ -512,13 +511,15 @@ angular.module('ui.scroll', []).directive('uiScrollViewport', function () {
512511

513512
// private function definitions
514513

515-
function injectValue(expression, value) {
514+
function createValueInjector(attribute) {
515+
var expression = $attr[attribute];
516+
var scope = viewportScope;
517+
var assign = undefined;
516518
if (expression) {
517519
var match = expression.match(/^(\S+)(?:\s+on\s+(\w(?:\w|\d)*))?$/);
518520
if (!match) throw new Error('Expected injection expression in form of \'target\' or \'target on controller\' but got \'' + expression + '\'');
519521
var target = match[1];
520522
var controllerName = match[2];
521-
var scope = viewportScope;
522523
if (controllerName) {
523524
var candidate = viewport;
524525
scope = undefined;
@@ -532,25 +533,13 @@ angular.module('ui.scroll', []).directive('uiScrollViewport', function () {
532533
}
533534
if (!scope) throw new Error('Failed to locate target controller \'' + controllerName + '\' to inject \'' + target + '\'');
534535
}
535-
$parse(target).assign(scope, value);
536-
/*
537-
let scope = viewportScope;
538-
let s = viewportScope;
539-
let i = expression.indexOf('.');
540-
if (i>0) {
541-
let ctrlName = expression.slice(0, i);
542-
while (s !== $rootScope) {
543-
if (s.hasOwnProperty(ctrlName) && angular.isFunction(s[ctrlName])) {
544-
scope = s;
545-
expression = expression.slice(i+1);
546-
break;
547-
}
548-
s = s.$parent;
549-
}
550-
}
551-
$parse(expression).assign(scope, value);
552-
*/
536+
assign = $parse(target).assign;
553537
}
538+
return function (value) {
539+
if (self !== value) // just to avoid injecting adapter reference in the adapter itself. Kludgy, I know.
540+
self[attribute] = value;
541+
if (assign) assign(scope, value);
542+
};
554543
}
555544

556545
function applyUpdate(wrapper, newItems) {

src/ui-scroll.js

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,13 @@ angular.module('ui.scroll', [])
422422
function Adapter($attr, viewport, buffer, adjustBuffer) {
423423
const viewportScope = viewport.scope() || $rootScope;
424424
let disabled = false;
425+
let self = this;
425426

426-
injectValue($attr.adapter, this);
427+
createValueInjector('adapter')(self);
428+
let topVisibleInjector = createValueInjector('topVisible');
429+
let topVisibleElementInjector = createValueInjector('topVisibleElement');
430+
let topVisibleScopeInjector = createValueInjector('topVisibleScope');
431+
let isLoadingInjector = createValueInjector('isLoading');
427432

428433
// Adapter API definition
429434

@@ -468,8 +473,7 @@ angular.module('ui.scroll', [])
468473
};
469474

470475
this.loading = (value) => {
471-
this.isLoading = value;
472-
injectValue($attr.isLoading, value);
476+
isLoadingInjector(value);
473477
};
474478

475479
this.calculateProperties = () => {
@@ -487,12 +491,9 @@ angular.module('ui.scroll', [])
487491
topHeight += itemHeight;
488492
} else {
489493
if (isNewRow) {
490-
this.topVisible = item.item;
491-
this.topVisibleElement = item.element;
492-
this.topVisibleScope = item.scope;
493-
injectValue($attr.topVisible, item.item);
494-
injectValue($attr.topVisibleElement, item.element);
495-
injectValue($attr.topVisibleScope, item.scope);
494+
topVisibleInjector(item.item);
495+
topVisibleElementInjector(item.element);
496+
topVisibleScopeInjector(item.scope);
496497
}
497498
break;
498499
}
@@ -501,14 +502,16 @@ angular.module('ui.scroll', [])
501502

502503
// private function definitions
503504

504-
function injectValue(expression, value) {
505+
function createValueInjector(attribute) {
506+
let expression = $attr[attribute];
507+
let scope = viewportScope;
508+
let assign = undefined;
505509
if (expression) {
506510
let match = expression.match(/^(\S+)(?:\s+on\s+(\w(?:\w|\d)*))?$/);
507511
if (!match)
508512
throw new Error('Expected injection expression in form of \'target\' or \'target on controller\' but got \'' + expression + '\'');
509513
let target = match[1];
510514
let controllerName = match[2];
511-
let scope = viewportScope;
512515
if (controllerName) {
513516
let candidate = viewport;
514517
scope = undefined;
@@ -523,25 +526,14 @@ angular.module('ui.scroll', [])
523526
if (!scope)
524527
throw new Error('Failed to locate target controller \'' + controllerName + '\' to inject \'' + target + '\'');
525528
}
526-
$parse(target).assign(scope, value);
527-
/*
528-
let scope = viewportScope;
529-
let s = viewportScope;
530-
let i = expression.indexOf('.');
531-
if (i>0) {
532-
let ctrlName = expression.slice(0, i);
533-
while (s !== $rootScope) {
534-
if (s.hasOwnProperty(ctrlName) && angular.isFunction(s[ctrlName])) {
535-
scope = s;
536-
expression = expression.slice(i+1);
537-
break;
538-
}
539-
s = s.$parent;
540-
}
541-
}
542-
$parse(expression).assign(scope, value);
543-
*/
529+
assign = $parse(target).assign;
544530
}
531+
return (value) => {
532+
if (self !== value) // just to avoid injecting adapter reference in the adapter itself. Kludgy, I know.
533+
self[attribute] = value;
534+
if (assign)
535+
assign(scope, value);
536+
};
545537
}
546538

547539
function applyUpdate(wrapper, newItems) {

0 commit comments

Comments
 (0)