简体   繁体   English

返回q.promise-淘汰赛/应用程序

[英]Return a q.promise - knockout/app

I'm building a mobile app that calls an API. 我正在构建一个调用API的移动应用。 An endpoint of the API gets called on app login and I see the results load in the DOM. API的端点在应用程序登录时被调用,我看到结果加载到DOM中。 I want to defer or promise the output/result for when I then click on a button to open another view/page inside the app. 当我单击按钮以在应用程序内打开另一个视图/页面时,我想推迟或保证输出/结果。 At the moment the app loads the endpoint twice when the API is called at the beginning and on the tap of the button to open up the desired page view. 目前,当应用程序在开始时调用API并点击按钮以打开所需的页面视图时,该应用程序会两次加载端点。

The problem I am having is getting the result packaged as a promise and then sent to the viewmodel ready for knockout to pass to my view. 我遇到的问题是将结果打包为Promise,然后将其发送到ViewModel,以便进行剔除以传递到我的视图。

The promise services the app uses are "q" and also has "promisehelper". 该应用程序使用的Promise服务为“ q”,还具有“ promisehelper”。 Not familiar with any and how I should be using them. 不熟悉任何内容以及如何使用它们。

Dataservice.js runs at app load - (this calls a function that builds the endpoint of the API) Dataservice.js在应用程序加载时运行-(这将调用用于构建API端点的函数)

 function getHelpText(companyName, userName, password) {
        return q.when(home.helpTextGet(company.name, company.userName, company.password));
    }

Viewmodel.js - (needs to call the promise from the data service) Viewmodel.js-(需要从数据服务调用promise)

        var MyText = ko.observable();

        var company = shell.authenticatedCompany();
        return q.getHelpText()
            .then(function(data) {
                if (!data) {
                    MyText(document.getElementById('no-help').innerHTML = '<div class="flex-item"><p>Request help from home Support:<br /><a href="mailto:support@home.com" class="low-profile-btn btn btn-lg"><i class="fa fa-info-circle"></i>&nbsp;Contact Home Support</a></p></div>');
                } else {
                    MyText(data);
                }
            });
        return {
            MyText: MyText
        };
        });

View.html - displays result on page/view View.html-在页面/视图上显示结果

<section class="help-text">
<div class="flex-container">

    <div id="no-help" class="help-content" data-bind="html: MyText"></div>

</div>

You can just put returned data to the MyText and use "if" and "ifnot" bindings in the markup: 您可以将返回的数据放到MyText中,并在标记中使用“ if”和“ ifnot”绑定:

Code: 码:

   var MyText = ko.observable();

    var company = shell.authenticatedCompany();
    return q.getHelpText()
        .then(function(data) {
            MyText(data);
        });
    return {
        MyText: MyText
    };
    });

Markup: 标记:

<section class="help-text">
<div class="flex-container">
    <!-- ko if: MyText -->
    <div id="no-help" class="help-content" data-bind="html: MyText"></div>
    <!-- /ko -->
    <!-- ko ifnot: MyText -->
    <div id="no-help" class="help-content">
    <div class="flex-item">
        <p>Request help from home Support:<br />
        <a href="mailto:support@home.com" class="low-profile-btn btn btn-lg"><i class="fa fa-info-circle"></i>&nbsp;Contact Home Support</a>
        </p>
    </div>
    </div>
    <!-- /ko -->
</div>

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

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