[英]javascript, create window in chrome web app, how to include a promise for successfully calling a function of the new window?
I want to create a window and when it is created, run a function that has been pre-loaded. 我要创建一个窗口,并在创建窗口时运行已预加载的函数。 I do have to wait until the window is created, otherwise the function is not available and fails. 我必须等到创建窗口之后,否则该功能将不可用并失败。 And I'd say I can do it with a promise. 我会说我可以兑现承诺。
How to do it? 怎么做? Here there's the code with my attempt to do it 这是我尝试执行的代码
one().then(function() {
new_panel.contentWindow.load_content(something);
})
function one() {
var promise = chrome.app.window.create('empty.html',
{
id: 'protein_panel',
outerBounds: {
width: 300,
height: 800,
},
},
function(createdWindow) {
createdWindow.contentWindow.load_content = function(data) {
createdWindow.contentWindow.document.getElementById('loading_content').appendChild(data);
}
new_panel = createdWindow;
});
return promise;
}
It complains about the then of the one() function: Uncaught TypeError: Cannot read property 'then' of undefined 它抱怨那个one()函数的那时:Uncaught TypeError:无法读取未定义的属性'then'
UPDATE: Slightly modified code, still doesn't work, I've used the promise from angular since I'm using it 更新:稍加修改的代码,仍然无法正常工作,因为我一直在使用我的承诺
one().then(function() {
new_panel.contentWindow.load_content(something);
})
function one() {
return chrome.app.window.create('empty.html',
{
id: 'protein_panel',
outerBounds: {
width: 300,
height: 800,
},
},
function(createdWindow) {
createdWindow.contentWindow.load_content = function(data) {
createdWindow.contentWindow.document.getElementById('loading_content').appendChild(data);
}
new_panel = createdWindow;
var deferred = $q.defer();
deferred.resolve();
console.log(deferred.promise)
return deferred.promise;
});
}
In theory, deferred.promise should only be returned when it is resolved, but it is like the then() function is executed before it actually happens. 从理论上讲,deferred.promise仅应在解析后返回,但这就像then()函数在实际发生之前就已执行一样。 Why? 为什么?
UPDATE2: or another way to do the same (doesn't work either) UPDATE2:或执行相同操作的另一种方法(也不起作用)
one();
function one() {
chrome.app.window.create('empty.html',
{
id: 'protein_panel',
outerBounds: {
width: 300,
height: 800,
},
},
function(createdWindow) {
createdWindow.contentWindow.load_content = function(data) {
createdWindow.contentWindow.document.getElementById('loading_content').appendChild(data);
}
new_panel = createdWindow;
var deferred = $q.defer();
deferred.resolve();
console.log('first '+deferred.promise)
return deferred.promise;
}).then(function() {
console.log('second')
new_panel.contentWindow.load_content(something);
});
}
The log 'second' is printed before 'first'. 在“第一”之前打印日志“第二”。 Is there anything wrong with the code? 代码有什么问题吗?
You want to synchronously return a promise from one()
and resolve the promise with the new window once it has been created: 您想同步地从one()
返回一个promise,并在创建新窗口后用新窗口解析promise:
one().then(function(createdWindow) {
// Window has been created
createdWindow.contentWindow.load_content(something);
})
function one() {
// create a promise
var deferred = $q.defer();
chrome.app.window.create('empty.html',
{
id: 'protein_panel',
outerBounds: {
width: 300,
height: 800,
},
},
function(createdWindow) {
// resolve our promise and pass the window to the `then` block
deferred.resolve(createdWindow)
});
// synchronously return a promise
return deferred.promise;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.