Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

ac.partial.render() throws error for handlebar custom helper #1306

Closed
jithine opened this issue Dec 13, 2013 · 14 comments
Closed

ac.partial.render() throws error for handlebar custom helper #1306

jithine opened this issue Dec 13, 2013 · 14 comments

Comments

@jithine
Copy link

jithine commented Dec 13, 2013

I have a handlebar helper which is added via

 ac.helpers.set('generateSelect', generateSelectHelper);

When running

148             ac.partial.render(newData, 'new-tasks', function (err, html) {
149                 Y.log(err);
150                 Y.log(html);
151                 ac.done(html);
152             });

I get this error

Uncaught Error: Could not find property 'generateSelect'

Content of new-tasks.hb.html

    1 <div id="{{mojit_view_id}}">
    2     {{#each data}}
    3         {{> task }}
    4     {{/each}}
    5 </div>

Contents of ./partials/task.hb.html

...
  4             <div class="content-row task-data pure-g">
  5                 <div class="drop-down-container pure-u-1-6">
  6                     {{{generateSelect tasks "task"}}}
  7                 </div>
  8            </div>
...

On an additional note ac.partial.render() does not accept a handle bar view partial located inside partials/ folder which restricts it's use. Eg:

 ac.partial.render(newData, './partials/task', ...

Gives an error saying cannot find ./partials/task'

@caridy
Copy link
Contributor

caridy commented Dec 13, 2013

Hey, this is a limitation of handlebars and not an issue in mojito. Handlebars will not propagate context from parent to a partial, that's why all functions and data available at the regular template will not be available into the partial unless you use ../something to reference to the parent context. Here is the discussion about the actual fix in the next version of handlebars, which includes some of the workaround:

handlebars-lang/handlebars.js#182

@jithine
Copy link
Author

jithine commented Dec 13, 2013

@caridy But it does work when I use ac.done() and the associated view uses the partial. ie instead of ac.partial.render () this works

150             ac.done({data : newData }, {
151                 view :  {
152                     name: "new-tasks"
153                 }
154             });

Then what is the purpose of ac.partial ? From the naming it suggests that it's for manipulating partials but it cannot load html inside partials directory. Documentation - http://developer.yahoo.com/cocktails/mojito/api/classes/Partial.common.html#method_render - says for ac.partial.render() view has to be views folder, which makes me conclude that ac.partial is not for handlebar partials.

My use-case if to instantiate this partial with new data, like in YUI template - http://yuilibrary.com/yui/docs/template/

@caridy
Copy link
Contributor

caridy commented Dec 16, 2013

@jithin1987 yes, now I understand what you want to do. First of all: ac.partial has nothing to with handlebars partials, it is a partial rendering as opposite to ac.done() which does a full rendering releasing the control from the controller. Basically, when calling ac.partial.render(data, 'new-tasks', callback), it is equivalent to call ac.done(data, { view: { name: 'new-tasts' } }), as you described, but with the ability to control the output before releasing the control.

Anyway, there is a bug in line 66 in the partial addon where the second argument should be just instance instead of instance.type:
https://github.com/yahoo/mojito/blob/develop/lib/app/addons/ac/partial.common.js#L66

Feel free to send a PR or wait for us to get to that. /cc @lzhan

@jithine
Copy link
Author

jithine commented Dec 16, 2013

@caridy Thank you very much for clarifying. One doubt I still have with partials is that, in Mojito is it possible to instantiate a view partial with data and render it, without having to include it in a view first ? ie in view.name can I use partial/task instead of using new-tasks which includes the partial task

@caridy
Copy link
Contributor

caridy commented Dec 16, 2013

@jithin1987 yes, path/to/filename within the views folder could be used to call ac.done() but also ac.partial.render(). In this case, it is ok to call partials/task, if that doesn't work for you, it might be a bug.

@lzhan
Copy link
Contributor

lzhan commented Dec 18, 2013

According to Mojito API: http://developer.yahoo.com/cocktails/mojito/api/classes/Partial.common.html#method_render, only putting partial view file directly under views dir is supported. I checked ac.partial.render(newData, 'task', ...) works fine. ac.partial.render(newData, './partials/task', ...) is not supported currently.

lzhan pushed a commit to lzhan/mojito that referenced this issue Dec 20, 2013
@lzhan
Copy link
Contributor

lzhan commented Dec 20, 2013

Just verified: ac.partial.render(newData, 'partials/task', ...) should works (don't use './partials/task'). Plz try.

@caridy
Copy link
Contributor

caridy commented Jan 9, 2014

@jithin1987 any update on this?

@jithine
Copy link
Author

jithine commented Jan 10, 2014

@caridy @lzhan This still does not work for me.

199                 ac.partial.render(
200                     currentData.comments,
201                     'partials/message',
202                     function(error, result) {
203                         Y.log(error);
204                         Y.log(result);
205                         ac.done(result);
206                     }
207                 );

Log Output

debug: View "partials/message" not found
debug: undefined

I do have this file partials/message.hb.html
My Mojito version is 0.7.5

@jithine
Copy link
Author

jithine commented Jan 10, 2014

@caridy @lzhan

I think I figured out what is happening, if I use any path other than partials it works.
Eg: partial/message.hb.html works. But I wanted to use partials because this way I can make my index.html include the partial in it and based on a user action I can refresh that part by using partial.render()

So my options here are to make this part of my mojit a child mojit or use a different view to have my content rendered there.

@jithine
Copy link
Author

jithine commented Jan 10, 2014

And the easiest fix here for me is to do cp -r partials/ partial or ln -s partials partial

@lzhan
Copy link
Contributor

lzhan commented Jan 10, 2014

@jithin1987 @caridy : I just checked. partials/ doesn't work. I will check what is going on there.

@caridy
Copy link
Contributor

caridy commented Jan 10, 2014

Yes, I remember I added a rule to treat files under partials/ as a different kind of views, therefore they are not available has views to avoid the duplication specially in Shaker, where all files are going to be aggregated. We need to decide what to do in this case. My initial thoughts are:

  • keep the current schema
  • add a rule in the template lookup routine to deal with partials/* if they really want to render an individual partial as a regular template.

I will sync up with the team later today and we can make a decision on this. @jithin1987 thanks again for the help to get the to bottom of this.

lzhan pushed a commit that referenced this issue Jan 22, 2014
Fix issue #1306: ac.partial.render() throws error for handlebar custom helper
lzhan pushed a commit to lzhan/mojito that referenced this issue Jan 24, 2014
lzhan pushed a commit that referenced this issue Jan 24, 2014
Add this fix to next branch: Fix issue #1306: ac.partial.render() throws error for handlebar custom helper
@lzhan
Copy link
Contributor

lzhan commented Jan 29, 2014

Fix is available in mojito 0.8.3.

@lzhan lzhan closed this as completed Jan 29, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants