HenrikJoreteg / ICanHaz.js

A clean solution for templating with Mustache.js and jQuery or Zepto
icanhazjs.com
Other
838 stars 132 forks source link

Loading html fragments & template name issue #44

Closed tyaakow closed 12 years ago

tyaakow commented 12 years ago

Cheers!

First, kudos for nice library. Setting it up was pretty much plug and play.

Now the issue I'm encountering: I'm loading external files/html fragments/strings from the server with require.js. In those files there's just raw html fragments, which are not enclosed into any script tags. They are just raw strings. I load them via require.js text plugin, and it returns just raw string ( 'text!templ/home/intro.html' is how I load the template ).

After that, I just use that text/html fragment like this: ich.addTemplate('person', loadedHtmlText); content = ich.person(dataobject); this.el.html(content);

Am I doing this properly? 'person' is not either previously declared anywhere, nor is it used as id of any script tags within templates, since these html fragments have none, they are just raw html.

Anyway, the code is rendered as expected, it all works fine, however the console throws an error: [19:39:10.212] Invalid name: person. @ http://localhost/projekt/test1/js/libs/ICanHaz.js:472

I'm using the latest repo version, converted with r.js command line tool into require.js module. (I doubt it's the conversion to blame)

So, I'd just like to hear opinion.

I looked into code, it seems that on line 472, code that creates the issue is a test wether the name/string already exists? However, my code doesn't have any other, same, already registered template/name.

tyaakow commented 12 years ago

It seems the issue was about re-rendering, and the name already existed. Conditional assignment did the job.

ErikGoldman commented 12 years ago

sorry, can you explain exactly how you fixed this? I'm having the same issue.

tyaakow commented 12 years ago

Well, the point was in ajax tabs which repeated adding of "person" which in browsers memory already existed. So I just added (inside backbone view) conditional like this:

    el: $("#mcont"),
    render: function(){
        if (_.isUndefined(ich.person)){ 
            ich.addTemplate('person', personTemplate);
        };
        content = ich.person();
        this.el.html(content);

I obviously forgot since it's all one page in browser, the template was already defined once, and that caused the error. (the error only appeared when re-rendering a tab > re-running backbone view)

LeonardoGentile commented 11 years ago

Thank @tjankov your last comment helped me out!