简体   繁体   中英

How to refresh handlebars template context/data?

I'm quite new to javascript templating and handlebars. I've got a thing like this:

<script id="group-container" type="text/x-handlebars-template">
    <fieldset class="group-container">
        <legend>Set {{set_number}}</legend>
        {{>hook_selector}}
        <button type="button" class="delete-gcontainer-button btn">{{text_blocks.buttons.delete_set}}</button>
    </fieldset>
</script>

The template is going to be instantiated several times. Each time the set_number data value is added 1, so we then have set 1, set 2, etc..

Well, the user can delete a set. But if he's got 3 sets and deletes the second, then he'll end up with set 1 and set 3. So I'd like to update set_number so the user will instead see set 1 and set 2. In other words, real time ordering.

Now, I could simply do it with some kind of js routine outside of the handlebars mechanics, but I was wondering if there's some built-in method or at least some preferred way of solving this (I think really common) issue.

To my way of thinking, this type of concern really belongs outside the template. I can see how the logic in the template might be what's initially triggered, when the user initiates an action, but I don't think of the template as properly doing anything beyond that. Maybe the template signals something, sets a timeout or something like that, to cause the necessary logic to begin happening ... the display-updating logic, that is, which would be asynchronous ... but I don't think of such things as properly being "the concern of" a template in the sense that they should be the ones doing it.

To my way of thinking, templates can contain some logic that's needed to update "themselves," and in a very direct way. If the decision of what to do is, shall we say, contextual ... "dependent on the bigger picture" ... to me, then that's really no longer a concern that "a template" should be directly dealing with. I just think that a template's concern more-or-less should begin and end with "my little piece of HTML."

So, frankly, I wouldn't look for "doing this in Handlebars." And, that's just my view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM