{ id: 'rE4aA', title: ' ', online: 3, recent: 0, // messages: [] // }
ngRepeat
{N} directive the number of chats (depending on the screen size). And I want to display a context menu that appears when you click the right mouse button on the title of any of the chats and allows you to move the selected chat to the place of another. This is how it looks like:ngRepeat
directive and applying a filter. For chats, the filter should be able to sort by the number of new messages (the recent property) and reduce the number of elements (chats) to the number {N} which is calculated from the size of the browser window. For the context menu, the same filter excludes the current element (the chat on which header was clicked). angular.module('app') .filter('opened', ['$rootScope', function($s){ return function(o){ console.log(' «opened»'); var count = $s.count; // , {N} return _(o) // Lo-Dash .sortBy('recent') // .reverse() // ( ) .first(count) // {N} .value() // } }]);
ngRepeat
directive, ngRepeat
see that in the console the message “The“ opened filter ”has been shown is shown twice. This means that half of the resources were wasted by the filter. Such convenience as the context menu doubled the rendering time of the actual state of the application. And if I continue to add functionality using the same data with filters, the situation will get even worse.-
(required) - the cached result of this function returns the memoize-
(optional) - the result of the function is the cache key (checks for uniqueness) function MyController($scope){ $scope.form = { input: {key:'', val:''}, // array: [ {key:'pear', val:''}, // {key:'melon', val:''}, {key:'ananas', val:''}, {key:'cherry', val:''} ], order: 'key', // key (2 key/val) check: false, // . (2 — true/false) add: function(){ // this.array.push(angular.copy(this.input)); this.filtered.cache = {} // }, filtered: _.memoize( // function(){ console.log(' : ' + this.order + ' ' + this.check); return _.sortBy(this.array, this.order) }, function(){ // - // return [this.order, this.check] } ) } }
<form name="myform" ng-app ng-controller="MyController"> <input type="text" required ng-model="form.input.key" placeholder="key"> <input type="text" required ng-model="form.input.val" placeholder="val"> <button ng-disabled="!myform.$valid" ng-click="form.add()"></button><br><br> <fieldset> <legend> : <select ng-model="form.order" ng-options="p for p in ['key', 'val']"></select> </legend> <div ng-repeat="el in form.filtered()"> {{el.key}} — "{{el.val}}" </div><br> <label> <input type="checkbox" ng-model="form.check"> - </label><hr> <pre>{{form.filtered()|json}}</pre> </fieldset> </form>
Ctrl
+ Shift
+ J
(relevant for the Chrome browser). We try to switch sorting and pull the flag. In the console, we see a maximum of 4 starts of the filter function (for each of the states). Adding a new element to the array - reset the cache and again verify the correct operation of this solution.Source: https://habr.com/ru/post/200130/
All Articles