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

Lazy view with fetch_params #494

Closed wvengen closed 8 years ago

wvengen commented 8 years ago

I'm trying to use a lazy-loaded view using fetch_params. With rendr-handlebars in the view:

{{view "foo" lazy=true collection_name='Products' fetch_params=params}}

and params something like {category_id: 12, q: "bar"} (a search query). I can't get this lazy view to work.

It seems that on the server-side, fetch_params are known. On the client, however, they are lost. Looking at BaseView#getAttributes, only non-object parameters are stored in the dom; neither does fetch_params become part of fetch_summary. When making fetch_params a json-string instead of an object, BaseView#fetchLazy complains.

Am I the first to use this functionality, or am I missing something?

saponifi3d commented 8 years ago

You need to JSON.stringify the params because it's going to just say "Object object" as the fetch_params otherwise

wvengen commented 8 years ago

Thanks.

For others reading this: somehow using rendr-handlebar's json helper didn't work. I needed to cast it to a String or else the resulting fetch params in the view's data attribute would still be empty.

{{view "foo/list" lazy=true model_name="Product" fetch_params=(jsons product_query}}

with product_query a simple object like {q: 'bar'}, and in my handlebars helpers:

   /*
    * Return object serialized as JSON.
    *
    * Same as rendr-handlebars' `json` but also cast to a string. Somehow this
    * is needed or else it isn't serialized.
    */
   jsons: function(object, spacing) {
     return String(JSON.stringify(object, null, spacing || 'null'));
   }