Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use safe digest in order to avoid $apply already on progress error #223

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
node_modules
test/draft.js
test/draft.js
/.idea/
48 changes: 28 additions & 20 deletions dist/angular-vs-repeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else i
* into a component, where the user thinks he has all the elements rendered and all he needs to do is scroll (without any kind of
* pagination - which most users loath) and at the same time the browser isn't overloaded by that many elements/angular bindings etc.
* The directive renders only so many elements that can fit into current container's clientHeight/clientWidth.
* LIMITATIONS:
* LIMITATIONS:
* - current version only supports an Array as a right-hand-side object for ngRepeat
* - all rendered elements must have the same height/width or the sizes of the elements must be known up front
* USAGE:
* USAGE:
* In order to use the vsRepeat directive you need to place a vs-repeat attribute on a direct parent of an element with ng-repeat
* example:
* <div vs-repeat="options">
* <div ng-repeat="item in someArray">
* <!-- content -->
* </div>
* </div>
* or:
* or:
* <div vs-repeat="options">
* <div ng-repeat-start="item in someArray">
* <!-- content -->
Expand All @@ -44,21 +44,21 @@ function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else i
* <!-- content -->
* </div>
* </div>
* You can also measure the single element's height/width (including all paddings and margins), and then speficy it as a value
* You can also measure the single element's height/width (including all paddings and margins), and then speficy it as a value
* of the option's `size` property. This can be used if one wants to override the automatically computed element size.
* example:
* <div vs-repeat="{size: 50}"> <!-- the specified element height is 50px -->
* <div ng-repeat="item in someArray">
* <!-- content -->
* </div>
* </div>
* IMPORTANT!
* - the vsRepeat directive must be applied to a direct parent of an element with ngRepeat
* IMPORTANT!
* - the vsRepeat directive must be applied to a direct parent of an element with ngRepeat
* - the value of vsRepeat attribute is the single element's height/width measured in pixels. If none provided, the directive
* will compute it automatically
* OPTIONAL PARAMETERS (attributes):
* OPTIONAL PARAMETERS (attributes):
* vs-repeat-container="selector" - selector for element containing ng-repeat. (defaults to the current element)
* OPTIONS:
* OPTIONS:
* Options shall be passed as an object to the `vs-repeat` attribute e.g.: `<div vs-repeat="{scrollParent: 'window', size: 20}"></div>`
*
* Available options:
Expand All @@ -67,14 +67,14 @@ function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else i
* `offset-after` - bottom/right offset in pixels (defaults to 0)
* `scroll-margin` - how many pixels ahead should elements be rendered while scrolling
* `latch` - if true, elements will be rendered gradually but won't be removed when scrolled away (defaults to false)
* `size` - a property name of the items in collection that is a number denoting the element size (in pixels)
* `size` - a property name of the items in collection that is a number denoting the element size (in pixels)
* `autoresize` - use this attribute without vs-size and without specifying element's size. The automatically computed element style will
* readjust upon window resize if the size is dependable on the viewport size
* `scrolled-to-end` - callback will be called when the last item of the list is rendered
* `scrolled-to-end-offset` - set this number to trigger the scrolledToEnd callback n items before the last gets rendered
* `scrolled-to-beginning` - callback will be called when the first item of the list is rendered
* `scrolled-to-beginning-offset` - set this number to trigger the scrolledToBeginning callback n items before the first gets rendered
* EVENTS:
* EVENTS:
* - `vsRepeatTrigger` - an event the directive listens for to manually trigger reinitialization
* - `vsRepeatReinitialized` - an event the directive emits upon reinitialization done
*/
Expand Down Expand Up @@ -161,6 +161,14 @@ function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else i
printDeprecationWarning($element, "".concat(attrname, " attribute is deprecated. Pass the options object to vs-repeat attribute instead https://github.com/kamilkp/angular-vs-repeat#options"));
}

function safeDigest(scope) {
var phase = scope.$root.$$phase;

if (phase !== "$apply" && phase !== "$digest") {
scope.$digest();
}
}

var defaultOptions = {
latch: false,
preserveLatchOnRefresh: false,
Expand Down Expand Up @@ -381,8 +389,8 @@ function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else i
reinitialize();
autosizingRequired = false;

if ($scope.$root && !$scope.$root.$$phase) {
$scope.$digest();
if ($scope.$root) {
safeDigest($scope);
}
}
} else {
Expand Down Expand Up @@ -419,7 +427,7 @@ function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else i
var pos = $scrollParent[0][scrollPos];

if (updateInnerCollection()) {
$scope.$digest();
safeDigest($scope);

if (options._ensureScrollIntegrity) {
$scrollParent[0][scrollPos] = pos;
Expand All @@ -434,13 +442,13 @@ function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else i
autosizingRequired = true;
getFromMeasured();

if ($scope.$root && !$scope.$root.$$phase) {
$scope.$digest();
if ($scope.$root) {
safeDigest($scope);
}
}

if (updateInnerCollection()) {
$scope.$digest();
safeDigest($scope);
}
}

Expand Down Expand Up @@ -478,8 +486,8 @@ function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else i
$afterContent.css(getLayoutProps(0));
$scope.$emit('vsRenderAllDone');

if ($scope.$root && !$scope.$root.$$phase) {
$scope.$digest();
if ($scope.$root) {
safeDigest($scope);
}
});
});
Expand Down Expand Up @@ -510,8 +518,8 @@ function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else i
if (ch !== _prevClientSize) {
reinitialize();

if ($scope.$root && !$scope.$root.$$phase) {
$scope.$digest();
if ($scope.$root) {
safeDigest($scope);
}
}

Expand Down
Loading