AngularJs search and pagination code duplication - angularjs

Some modules of the application I'm writing give the user the option to filter a dataset and paginate the results.
After getting everything to work as intended, I'm trying to refactor the code in order to remove code duplication.
Each and everyone of the afore mentioned modules share the same bit of logic for filtering and pagination.
I'm wondering which is the best practice to follow in order to remove that kind of duplication.
As of now I was thinking of creating a "filter and pagination" service with the following API:
Paginate(items,predicate,itemsPerPage)
Returns an object:
{
paginatedItems
nrOfPages
}
Predicate is called for each and every item passed in the collection, and it is used
to select items based on user preferences.
Now each module would take advantage of that API.
Is there a better, recomended way?
Thanks in advance for your help

Related

CouchDB: Filter Functions in Views

I'm wondering if it's possible to use Filter Functions when requesting a view something like:
http://localhost:5984/db/myView/myMap?filter=myFilter.
I'm going to use Complex Keys with separate views eventually but I'm wondering if this is possible. So far I tried but it's not showing me any differences.
According to the documentation it's not possible to do so. You can pass keys to query your view but you can't filter it. Filter can be applied to _changes aswell as in replications.

Handling multiple filters in angularjs

I am using ng-repeat to render a complex set of data in the UI. After receiving the data, I need to filter the data based on some checkboxes, sliders etc. Right now I will have 6 custom filters, based on time, price, and some checkboxes. I am using them like following
ng:repeat="response in searchResponse|filter:filter1(response)|filter:filter2(response)|filter:filter3(response)|filter:filter4(response)|filter:filter5(response)|filter:filter6(response)|limitTo:totalDisplayed
This works fine but I think this is a very expensive way of applying filters. I have noticed a considerable lag in performance in rendering after this. Is there a better way to handle filters here ? Considering the fact that I may have more such filters in future.
Please suggest. Thanks !
Instead of chaining filters, why not something like:
ng:repeat="response in searchResponse|filter:genericFilter(response, filterConditions)|limitTo:totalDisplayed
What you're doing is like chaining filters one after another, and that may not necessarily be needed – one filter that selects out the results based on filterConditions

CakePhp pagination and my own function

I have a big function in my model. It returns list of friends with their photos and profiles. There are two 'finds' with loops in body, so it is not easy.
Now I need to show this list with pagination.
Is threre any posibility to create pagination with my method?
There is no posibility to get this data in one find (or paginate) function.
Any ideas?
Yes, it is possible.
You can implement the paginate() and paginateCount() methods on your model, or include them in a behavior attached to your model. Behaviors implementing paginate and/or paginateCount should implement the method signatures defined below with the normal additional first parameter of $model:
Custom Query Pagination will do the same stuff you needed.

Why are the advantages or filter and orderBy?

It seems that AngularJS really puts an heavy emphasis on using filters and other ng directive in your view to filter and sort your data, instead of doing it manually in the model. Is there any reason for that, ie is it faster, cached, or something?
I want to show a list sorted for example, but I also want to access the sorted list for other purposes that are not view related. It's very easy if the list is directly sorted in the model, so I'm trying to understand if there is a drawback to doing it this way.
Thanks!
I don't see anything wrong with pre-sorting the data if it makes sense to you but here are some pros and cons for using Angular filters.
Pros:
Clear separation of the view and model. The model/controller does not need to be aware of or include code related to how the data will be displayed/sorted/filtered
Since filters are executed as the model changes the orderBy filter can automatically sort as items are added to an array via the UI
Filters can be used to format data for display (the currency filter for instance) as well as modify the DOM adding/removing items (the filter filter for instance) without modifying the underlying model data
Promotes reuse of commonly used built in or custom filter functions
Cons:
Poorly written filter function can cause performance issues. You can see a purposely contrived example in the AngularJS Batarang video starting at 4:30. Any code (not just a filter) can be written poorly but it's not initially obvious how often a filter gets called.
Slightly confusing in that some filters act on a single number/string (currency filter) and some on arrays (orderBy filter)
Syntax to pass arguments and chaining of filters can also be a bit confusing
I'm sure there are many more pros/cons but hopefully this helps!

How do I track my parent find function call in beforeFind() for CakePHP

I am currently using CakePHP beforeFind() to filter my search queries based on the current logged in user. It seems great at the moment and everything is working smoothly, the only problem is that I need to know where the find function was initially called so that I can apply different query filters based on the parent find function call; is there anyway to achieve that?
That seems like coupling your models to your controllers/actions, which in general is a bad idea.
Try creating a custom method in your model instead. You can have whatever method signature you need, and just call find from within and return its results as needed.

Resources