简体   繁体   English

使用Javascript进行跨域模板化

[英]Cross-domain templating with Javascript

I'm currently building a Javascript library that can be used to easily create embeddable media based on the URL of a media file, and then be controlled using Javascript methods and events (think something like the Flash / Silverlight JW player ). 我目前正在构建一个Javascript库,可以用来根据媒体文件的URL轻松创建可嵌入媒体,然后使用Javascript方法和事件进行控制(想想像Flash / Silverlight JW播放器一样 )。

Of course, i could simply cat all the html tags from the Javascript library and send that to the browser: 当然,我可以简单地从Javascript库中获取所有html标签并将其发送到浏览器:

function player(url) {
    document.write('<object type="foo"><param name="something" value="bar">' + 
    <param name="source" value=" + url + '/></object>');
}

But i think this is a very ugly practice that tends to create unmanageable code that is unreadable when you review it a few weeks later. 但我认为这是一个非常丑陋的做法,往往会产生无法管理的代码,几周后你再查看它是不可读的。

So, a templating solution seems to be the way to go. 因此,模板解决方案似乎是要走的路。 I've been looking to EJS because it loads the templates using AJAX, so you can manage your templates in separate file instead of directly on the HTML page. 我一直在寻找EJS,因为它使用AJAX加载模板,因此您可以在单独的文件中管理模板,而不是直接在HTML页面上。

There's one 'gotcha' with that: my library needs to be completely cross-domain: the library itself could be located on foo.com while the serving site could be located on bar.com. 有一个'问题':我的图书馆需要完全跨域:图书馆本身可能位于foo.com,而服务网站可能位于bar.com上。 So if bar.com would want to add a media player using the library it needs to do an AJAX call to a template located on foo.com, which won't work because of the same-origin policy in browsers. 因此,如果bar.com想要使用该库添加媒体播放器,则需要对位于foo.com上的模板进行AJAX调用,由于浏览器中的同源策略,该模板无法工作。

AFAIK, there's no library that uses something like JSONP to read and write templates to get around this problem. AFAIK,没有库使用类似JSONP的东西来读写模板来解决这个问题。

Could anyone point me to a solution for this problem? 谁能指点我解决这个问题?

Answering my own question: you need some kind of server-side solution that delivers JSONP to solve this problem. 回答我自己的问题:你需要某种服务器端解决方案来提供JSONP来解决这个问题。 Let's say tou have a template on server foo.com, you could have a server-side script that answers to requests like these: 假设tou在服务器foo.com上有一个模板,你可以有一个服务器端脚本来回答这些请求:

http://foo.com/template/bar.html?callback=cb

Which would return: 哪个会回归:

cb({
    "html" : "<p>i'm a template!</p>"
});

Then you can use any template language you want and parse the template in your app. 然后,您可以使用所需的任何模板语言并在应用程序中解析模板。

Very late to this question, but I've come to believe that KnockOut JS is the best solution for third-party template rendering. 这个问题已经很晚了,但我开始相信KnockOut JS是第三方模板渲染的最佳解决方案。 If you are loading your JS onto a third-party's web pages, Knockout lets you very easily inject your data into their DOM, without having to hold the entire section of HTML as a template which you "render" (this is the paradigm that moustache and similar use). 如果您将JS加载到第三方的网页上,Knockout可以让您轻松地将数据注入到他们的DOM中,而无需将整个HTML部分保存为您“渲染”的模板(这是胡子的范例)和类似的用途)。

The code is simple - here's the third party's web page region you want to template: 代码很简单 - 这是您要模板化的第三方网页区域:

<p>First name: <strong data-bind="text: firstName"></strong></p>
<p>Last name: <strong data-bind="text: lastName"></strong></p>

After loading knockout js library itself, you then just present some data to knockout as a javascript "data model object" and it does the rest: 在加载knockout js库之后,你只需要将一些数据作为javascript“数据模型对象”提供给淘汰赛,然后完成剩下的工作:

// This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UI
function AppViewModel() {
    this.firstName = "Bert";
    this.lastName = "Bertington";
}

// Activates knockout.js
ko.applyBindings(new AppViewModel());

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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