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

Custom Watcher #36

Open
wants to merge 4 commits 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
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,35 @@ This index reflects the index of the corresponding object in the source collecti

That's all! Ehm, no. If you run your application now, you will notice that there is only one column. What is missing? Well, we have to define the configuration for the visual representation. And what is the best place for something like this? Yes, for sure! Your CSS file(s).

### Providing a custom function to build the watch listener

If you are expecting a lot of items or have complex objects you may want to provide a custom function to build a watch expression instead of asking angular to deep compare each object. The function should return a string and will be called for each item in `source`. The result will be a concatenated string from the result of each function.

Example:

```html
<div>
<ul deckgrid card-template="CardView" item-identifier-fn="getItemId" source="items">
...
</ul>
</div>
```

```javascript
$scope.items = [
{ id: 1, name: "Photo 1", properties: {...} },
{ id: 2, name: "Photo 2", properties: {...} },
{ id: 3, name: "Photo 3", properties: {...} },
{ id: 4, name: "Photo 4", properties: {...} },
];

$scope.getItemId = function (item) {
return item.id + "|";
}
```

The value used for the watch expression will be: `'1|2|3|4|'`.

## The grid configuration

The grid items will be distributed by your configured CSS selectors. An example:
Expand Down
45 changes: 40 additions & 5 deletions angular-deckgrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ angular.module('akoenig.deckgrid').factory('DeckgridDescriptor', [
'</div>';

this.scope = {
'model': '=source'
'model': '=source',
'itemIdentifierFn': '='
};

//
Expand Down Expand Up @@ -185,7 +186,16 @@ angular.module('akoenig.deckgrid').factory('Deckgrid', [
//
// Register model change.
//
watcher = this.$$scope.$watch('model', this.$$onModelChange.bind(this), true);
var watchExpression = !scope.itemIdentifierFn ?
'model' :
function() {
var result = '';
for (var i = 0; i < scope.model.length; i++) {
result += scope.itemIdentifierFn(scope.model[i]);
}
return result;
};
watcher = this.$$scope.$watch(watchExpression, this.$$onModelChange.bind(this), true);
this.$$watchers.push(watcher);

//
Expand Down Expand Up @@ -368,8 +378,19 @@ angular.module('akoenig.deckgrid').factory('Deckgrid', [
Deckgrid.prototype.$$onModelChange = function $$onModelChange (newModel, oldModel) {
var self = this;

if (oldModel.length !== newModel.length) {
self.$$createColumns();
if (isString(newModel) || isArray(newModel)) {
if (oldModel.length !== newModel.length) {
self.$$createColumns();
} else {
for (var i = newModel.length - 1; i >= 0; i--) {
if (oldModel[i] !== newModel[i]) {
self.$$createColumns();
break;
}
}
}
} else {
$log.error('DeckGrid watch expression must return an array or string.');
}
};

Expand All @@ -386,10 +407,24 @@ angular.module('akoenig.deckgrid').factory('Deckgrid', [
}
};

function isString(value) {
return typeof value === 'string' ||
value && typeof value === 'object' && value.toString() === '[object String]' || false;
}

function isArray(obj) {
if (Array.isArray) {
return Array.isArray(obj);
}

return obj && typeof obj === 'object' && typeof obj.length === 'number' &&
obj.toString() === '[object Array]' || false;
}

return {
create : function create (scope, element) {
return new Deckgrid(scope, element);
}
};
}
]);
]);
2 changes: 1 addition & 1 deletion angular-deckgrid.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 37 additions & 3 deletions src/deckgrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,16 @@ angular.module('akoenig.deckgrid').factory('Deckgrid', [
//
// Register model change.
//
watcher = this.$$scope.$watch('model', this.$$onModelChange.bind(this), true);
var watchExpression = !scope.itemIdentifierFn ?
'model' :
function() {
var result = '';
for (var i = 0; i < scope.model.length; i++) {
result += scope.itemIdentifierFn(scope.model[i]);
}
return result;
};
watcher = this.$$scope.$watch(watchExpression, this.$$onModelChange.bind(this), true);
this.$$watchers.push(watcher);

//
Expand Down Expand Up @@ -229,8 +238,19 @@ angular.module('akoenig.deckgrid').factory('Deckgrid', [
Deckgrid.prototype.$$onModelChange = function $$onModelChange (newModel, oldModel) {
var self = this;

if (oldModel.length !== newModel.length) {
self.$$createColumns();
if (isString(newModel) || isArray(newModel)) {
if (oldModel.length !== newModel.length) {
self.$$createColumns();
} else {
for (var i = newModel.length - 1; i >= 0; i--) {
if (oldModel[i] !== newModel[i]) {
self.$$createColumns();
break;
}
}
}
} else {
$log.error('DeckGrid watch expression must return an array or string.');
}
};

Expand All @@ -247,6 +267,20 @@ angular.module('akoenig.deckgrid').factory('Deckgrid', [
}
};

function isString(value) {
return typeof value === 'string' ||
value && typeof value === 'object' && value.toString() === '[object String]' || false;
}

function isArray(obj) {
if (Array.isArray) {
return Array.isArray(obj);
}

return obj && typeof obj === 'object' && typeof obj.length === 'number' &&
obj.toString() === '[object Array]' || false;
}

return {
create : function create (scope, element) {
return new Deckgrid(scope, element);
Expand Down
3 changes: 2 additions & 1 deletion src/descriptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ angular.module('akoenig.deckgrid').factory('DeckgridDescriptor', [
'</div>';

this.scope = {
'model': '=source'
'model': '=source',
'itemIdentifierFn': '='
};

//
Expand Down