[英]passing parameter to ember in iframe
What is the best practice to passing parameter to ember.js from another application. 将参数从另一个应用程序传递到ember.js的最佳实践是什么。 For example, I need click on link in main page and pass parameter to ember in iframe.
例如,我需要单击主页上的链接,然后将参数传递给iframe中的ember。 I done it through cookies, and check value in loop, but it's a not good practice.
我是通过Cookie来完成的,并循环检查值,但这不是一个好习惯。
setInterval(function(){
Ember.$.getJSON('/url'+Ember.$.cookie("id")).then(function(response){...});
}, 500);
I would use window.postMessage() and window.on("message") system to connect the two applications. 我将使用window.postMessage()和window.on(“ message”)系统连接两个应用程序。
For example, let's say in your iFrame you have an ember app that can display a widget from collection and in your main app, you have an id selector. 例如,假设您在iFrame中有一个可以显示集合中的小部件的余烬应用程序,而在主应用程序中有一个ID选择器。 Here's how this would work.
这是如何工作的。
In your main app: 在您的主应用程序中:
App.IndexController = Ember.Controller.extend({
//... other stuff
actions: {
showWidget: function () {
var id = parseInt(this.get("widgetId"), 10),
iframe = $("#inner_page")[0];
iframe.contentWindow.postMessage(
JSON.stringify({ type: "action", args: ["showWidget", id]}),
"*");
}
}
});
widgetId
is the id you want to pass to iframe (can be tied to a text field, for example). widgetId
是您要传递给iframe的ID(例如,可以绑定到文本字段)。 Your iframe would have id "inner_page". 您的iframe的ID为“ inner_page”。 The second argument in the
postMessage
call is origin, you should look into securing that once you get the communication working. postMessage
调用中的第二个参数是origin,一旦通信开始工作,就应该确保其安全。
Code for the iframe app is more interesting: iframe应用程序的代码更有趣:
$(window).on("message", function(e) {
var message = JSON.parse(e.originalEvent.data),
handler = App.messageHandlers[message.type];
if (!handler) {
consolw.log("WARNING! Invalid action call!");
return;
}
handler(message);
});
App.messageHandlers = {
action: function (msg) {
if (App.activeController) {
App.activeController.send.apply(App.activeController, msg.args);
}
}
};
App.IndexRoute = Ember.Route.extend({
//...
setupController: function (controller, model) {
this._super(controller, model);
App.activeController = controller;
}
});
App.IndexController = Ember.Controller.extend({
//...
actions: {
showWidget: function (id) {
// update active widget, load route or whatever
}
}
});
A few notes: 一些注意事项:
Our message has a fixed "type" property. 我们的消息具有固定的“类型”属性。 Right now, this is always "action", but it could be useful if you wanted multiple protocols of communication.
现在,这始终是“动作”,但是如果您需要多种通信协议,则可能会很有用。
You will need some way to get the active controller or route to which to send the message. 您将需要某种方式来获取活动控制器或将消息发送到的路由。 In this example, I'm caching the active controller every time a new controller set up (
App.activeController
). 在此示例中,每次设置新控制器(
App.activeController
)时,我都会缓存活动控制器。 If you went with this model, this should probably be implemented as a Route Mixin. 如果使用此模型,则可能应将其实现为Route Mixin。 Not sure how well this will work in a larger application, but it's sufficient for this example.
不确定在大型应用程序中该方法将如何工作,但是对于此示例而言已足够。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.