Call a function inside an underscore template using backbone

前端 未结 3 1859
长情又很酷
长情又很酷 2021-01-11 10:11

Just a thing I try to do that would really simplify my life right now.

How can I do that :

This is my view in app file

    window.ArtView = B         


        
相关标签:
3条回答
  • 2021-01-11 10:26

    That's wrong. Think about the template as a string, html markup. You get it and replace some parts of it with the actual data. If you want to do some DOM manipulation they should be made after that. Let us know what you want to do in callFunction and we may guide you to the right place for that logic.

    0 讨论(0)
  • 2021-01-11 10:27

    this is how I did it , it works fine.

    template: _.template(templateText , {
                imports : {
                    check :function (val){
                        // blah blah
                        }
                    }
                }
            }),
    

    then in your html template

    <%= check('value') %>
    
    0 讨论(0)
  • 2021-01-11 10:28

    I believe you can call functions within the template as long as the object for the template has the function.

    render:function (eventName) {
        var output="blablbla";
        var data = _.extend({"output":output}, callFunction);
        $(this.el).html(this.template(data));
        return this;
    }
    

    then in your template:

    <%= callFunction() %>
    
    0 讨论(0)
提交回复
热议问题