Loading underscore.js templates from a separate file

六月ゝ 毕业季﹏ 提交于 2019-12-06 12:59:05

I personally use RequireJS to load my templates into a module but if you're looking for something else you can use Ajax.

As long as your templates are located on the same domain you can get them through an ajax request. I fail back to the following code if whomever using my widget isn't using an AMD compatible library:

$.ajax({
    url: root.WIDGET.BaseUrl + 'templates/widget.html',
    asynx: false, // synchonous call in case code tries to use template before it's loaded
    success: function (response) {
        widgetTemplate = response;
    }
});

This assumes you're using jQuery as well but the principle is the same if you're using something else.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!