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.
Related
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.
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
Help me to find best approach for managing Backbone views.
For example I have a collection of views MyCollectionView which consists of MyModelView - views of each model in the collection.
And what if I want to hide/show some models on the page?
Now I am following this way:
Use collection.each for every model
Inside of loop I call model function filter with some params
In this function I check model properties and call model.trigger 'hide' or model.trigger 'show'
And finally in the model view I use this.model.bind 'hide', this.hide, this where actually I use .hide() or .show()
This way seems me awful... Why do I need to do this long chain of functions and events. Does any simplest approach exist?
Thanks!
Your models shouldn't be telling views what they should do - they are supposed to represent data and shouldn't take part in controlling the application - so no wonders it feels wrong to you :)
The more elegant way would be to add a filter method to the MyCollectionView which would use underscore methods to filter the views you want to show/hide and do its job of well... doing that - picking which models should be shown. Then having hte array of matches just call a method for rendering the list and pass your array of models to it so it can render the views for matching models.
From my experience of creating such filters I can tell you that it might be much more efficient for longer lists to remove whole list do filtering on collection level and render again only the views which are matching the filter query. jQuery hide/show can be a bit taxing - though it's a concern you will have only with large amount of data/views.
Also! Take advantage of the underscore methods bound to the collection - you don't need to do collection.each(... you can just do
var matches = collection.filter(function(model) {
return /*matching condition*/;
});
(Also remember to use documentFragment when rendering lists and appending to the DOM pre-generated list of views rather then appending them one by one)
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.
I have loads of model functions returning different datasets already.
I would like to be able to paginate these without having to rewrite them all using the paginate method in the controller. Is there a tidy way of doing this?
Wouldn't this do the trick?
$this->set('dataSet', $this->paginate($verySpecialModelDataSet, $paginateOptions));
If you have many controllers, you could possibly make your calls by overriding beforeRender()
[http://book.cakephp.org/view/60/Callbacks] in AppController.