rendrjs / rendr

Render your Backbone.js apps on the client and the server, using Node.js.
MIT License
4.09k stars 312 forks source link

Re-fetching collection with params #361

Closed mikepuerto closed 10 years ago

mikepuerto commented 10 years ago

Hi,

Basically what I'm trying to do is a faceted search type search where once you click a checkbox, it updates the params object and then re-fetches the data for rendering.

I'm currently using the _{{view}}_ helper to load child views and passing it a collection with collection parameters. Those params get sent to the server via query string... How can I add/remove params to the params object and then refetch with a new query string? Is this possible?

brettshollenberger commented 10 years ago

@mikepuerto, I think you wouldn't want to re-run the controller's app#fetch; it seems like a lot of overhead to re-load part of your data. Instead just call fetch on the collection in your subviews. Recently I dealt with this problem by making separated components like a search bar and a list view, and passing params and/or additional constraints in through the Handlebars helpers. This PR allows you to make changes to the collection object and pass it to various helpers that need it, so they can update the collection, and the collection can respond to those events by updating the list view (https://github.com/rendrjs/rendr/pull/349).

My DOM ends up looking like this:

      {{searchbar
        collection=staff 
        constraints=constraints 
        placeholder="Search staff..."}}
    <div class="list">
      {{repeat "user in staff" template="users/detail"}}
    </div>
    {{paginate staff}}

Both paginate and searchbar are wrappers around the {{view}} helper, and they're just performing a fetch on the collection passed in, which updates the other views correspondingly. If you're familiar with Angular, I also have a video explaining the same topic using this approach (https://egghead.io/lessons/search-directive-with-rails). You have to do a bit more low-level work with Backbone, but in the end you can achieve the same effect.

mikepuerto commented 10 years ago

@brettshollenberger No, I wasn't intending to re-run the controller method... I'm just trying to figure out if I can modify the params object that gets created and re-fetch the collection. I'm still trying to wrap my head around how rendr fetches data. The view helper doesn't really have anything to do with it I guess... I think I'm going about it the wrong way....

A quick example of like what I'm trying to achieve, in a "regular" Backbone setting.... say I have a products collection and a params model. The model holds all my search params, then on change of the model, I would serialize it and pass it to the collection, then fire fetch() on the collection. Meanwhile there is a productList view that is listening for a reset event on the collection and will render once fetched. Make sense?

Do I have to use this.app.fetch() every time I want to fetch data?

Thanks, Mike