[英]Resolving a previously rejected promise
I know that the state of a promise is immutable, so once it is resolved/rejected, it stays at the state. 我知道承诺的状态是不可变的,因此一旦解决/拒绝了它,它就会停留在该状态。 But imagine the following scenario:
但是,请设想以下情形:
I am creating a scheduling application. 我正在创建一个调度应用程序。 You go on a given day, and make a request to the API to get the schedule.
您在给定的一天去,向API提出请求以获取时间表。 A promise is created while the API call is in progress.
在进行API调用时会创建一个Promise。 If a schedule is found, then the promise is resolved, and you see the schedule.
如果找到了时间表,那么诺言就解决了,您将看到时间表。 If no schedule is found the promise is rejected and you see a message say "No schedule found. Please create one first".
如果未找到时间表,则承诺将被拒绝,并且您会看到一条消息“找不到时间表。请先创建一个时间表”。
The markup of how these two messages show up is as follows: 这两个消息如何显示的标记如下:
<!-- The promise is passed to the wrapping div -->
<!-- While API is in progress, the loader directive will show an ajax loading icon -->
<!-- and will hide the content -->
<div loader="loader.promise" loader-channel="some-channel">
<!-- If loader is resolved, the the loader-success shows up -->
<div loader-success="some-channel">
<!-- If loader is rejected, the the loader-failure shows up -->
<div loader-failure="some-channel">
Now, I create a schedule, and want to show the newly created schedule without reloading the page. 现在,我创建一个日程表,并希望在不重新加载页面的情况下显示新创建的日程表。 I need to resolve the previous promise as this promise controls the different parts of markup.
我需要解决之前的承诺,因为该承诺控制标记的不同部分。
What can I do? 我能做什么?
Edit 编辑
I know that I could use the promise internally in my controller, and have a variable called scope.noSchedule
that is set to true or false. 我知道我可以在控制器内部使用
scope.noSchedule
,并将变量名为scope.noSchedule
设置为true或false。
However, I created the loader
directive so that I could easily add that directive to any component in my application. 但是,我创建了
loader
指令,以便可以轻松地将该指令添加到应用程序中的任何组件。 This way, all loaders of each component look the same, and also I don't need to reimplement it everytime I start a new component. 这样,每个组件的所有加载程序看起来都一样,而且每次启动新组件时也不需要重新实现它。
Don't access the promise directly in your markup, use it to set a variable or two in the scope and use those in the in markup. 不要在您的标记中直接访问promise,使用它在范围内设置一个或两个变量,并在in标记中使用它们。 Then you can have a method in your controller to call a new promise and rebind the markup.
然后,您可以在控制器中有一个方法来调用新的Promise并重新绑定标记。 Something like this...
像这样
var aPromise = scheduleService.getSchedule();
aPromise.then(
function (data) { $scope.Schedule = data; },
function () { $scope.scheduleError = true; });
$scope.tryAgain = function() {
aPromise = scheduleService.getSchedule();
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.