如何将 jQuery 数据处理更改为 openWeather API 的 Knockout 绑定

瑞森

我想使用 Knockout 数据绑定来更新我的 html div 元素中的天气 API 数据。目前,我使用 jQuery 来更新 DOM,但我更喜欢使用 Knockout。我还想动态更改位置 zip,以便天气特定于我 div 中的那个位置。我有一个位置数组。

这是我的代码:html:

var zip = locations[0].zipcode;
var myOpenWeatherAPIKey = 'xxxxxxxxxxxxxxxxxxxx';
var openWeatherMapUrl = "http://api.openweathermap.org/data/2.5/weather?zip=" + zip + ",us&APPID=" + myOpenWeatherAPIKey + "&units=imperial";
console.log(zip);
//using JSON method for retrieving API data
$.getJSON(openWeatherMapUrl, function(data) {
    var parameters = $(".weather-data ul");
    var iconCode = data.weather[0].icon;
    var iconDescription = data.weather[0].main;
    var iconUrl = "http://openweathermap.org/img/w/" + iconCode + ".png";
    detail = data.main;
    windspd = data.wind;
    parameters.append('<li>Temp: ' + Math.round(detail.temp) + '° F <br></li>');
    parameters.append('<li><img style="width: 25px" src="' + iconUrl + '">  ' + iconDescription + '</li>');
}).fail(weatherError = function(e) {
    $(".weather-data").append("OpenWeatherAPI is unable to load!");
});
<div id="open-weather" class="open-weather">
    <div id="weather-data" class="weather-data">
        <p>  <br> Current Weather</p>
        <ul id="weather-items" class="weather-items">
        </ul>
    </div>
</div>

谢谢,

乔威尔逊

您可以编写自定义 KO 绑定自定义 KO 组件来封装标记、API 密钥、jQuery 和 XHR 调用。

对于您的代码和用例,组件听起来很合适。那么你的消费标记可能看起来像

<weather params="zip: myZipCode"></weather>

您的消费页面的视图模型中的myZipCodeako.observable哪里

更新 1

在代码中添加了用于封装在 KO 组件中的粗略代码。

更新 2

将 jquery DOM 引用移出 KO 组件代码,以便组件模板使用 KO 绑定。添加了消费页面视图模型以显示完整示例。

function openWeatherComponentViewModel(params) {
  var self = this;
  
  self.zip = ko.observable(ko.unwrap(params.zip));
  self.temperature = ko.observable();
  self.iconCode = ko.observable();
  self.iconUrl = ko.pureComputed(function() {
    return "http://openweathermap.org/img/w/" + self.iconCode() + ".png";
  });
  self.iconDescription = ko.observable();
  self.hasWeather = ko.observable(false);
  self.errorMessage = ko.observable();
  
  var apiKey = 'xxxxxxxxxxxxxxxxxxxx';
  var url = "https://api.openweathermap.org/data/2.5/weather?zip=" + self.zip() + ",us&APPID=" + apiKey + "&units=imperial";  
  $.getJSON(url, function(data) {
    self.temperature(Math.round(data.main.temp));
    self.iconCode(data.weather[0].icon);
    self.iconDescription(data.weather[0].main);
    self.hasWeather(true);
  }).fail(function(error) {
    self.hasWeather(false);
    self.errorMessage("OpenWeatherAPI is unable to load! " + error.responseJSON.message);
  });
}

ko.components.register('weather-component', {
  viewModel: openWeatherComponentViewModel,
  template: `<div id="open-weather" class="open-weather">
                <div id="weather-data" class="weather-data" data-bind="if: hasWeather()">
                    <p>Current Weather for Zip Code <span data-bind="text: zip"></span></p>
                    <ul id="weather-items" class="weather-items">
                      <li>Temp: <span data-bind="text: temperature"></span>° F</li>
                      <li><img style="width: 25px" data-bind="attr: { src: iconUrl }"><span data-bind="text: iconDescription"></span></img></li>
                    </ul>
                </div>
                <div id="weather-data" class="weather-data" data-bind="if: !hasWeather()">
                    <span data-bind="text: errorMessage"></span>
                <div>
            </div>`
});

function myConsumingPageViewModel() {
  var self = this;
  self.myZipCode = ko.observable("80130");
}

var vm = new myConsumingPageViewModel();

ko.applyBindings(vm);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<weather-component params="zip: myZipCode"></weather-component>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用openweather API将json加载到熊猫数据框

如何将“绑定”脚本更改为“独立”脚本?

Xamarin-如何将数据从Rest API绑定到StackLayout NOT ListView

如何在数据表中绑定Openweather地图API数据?

如何将动态复选框值绑定到对象上的Knockout observableArray?

如何将Web API数据绑定到Angular 6中的表单选择选项

如何将数据放入嵌套数组并将其绑定到HTML Knockout.js

OpenWeather Api将JavaScript提取与用户输入配合使用,将以前的数据保留在HTML页面中

Knockout.js数据绑定到ViewModel

如何将选定的值从jQuery datepicker绑定到Knockout Observable

如何将Unity的super-t绑定更改为其他绑定?

更改Knockout.js绑定

使用jQuery的Knockout.js数据绑定

使用Knockout JS的JQuery UI Multiselect-使用帮助程序插件进行数据绑定

如何使用JavaScript / Jquery / Knockout JS将数字数组绑定到Range Slider

如何将范围从jQuery绑定更改为onClick?

如何将Knockout绑定应用于<html>元素?

如何将GUD断点键绑定更改为旧的

如何将事件Jquery绑定到页面?

jQuery:如何将 onClick 事件绑定到 DataTable 行?

如何用simpledate格式解析openweather数据

Knockout 3+ 如何将 jquery 移动单选按钮绑定到淘汰赛?

OpenWeather API 的错误

如何将数据绑定到多选?

如何将api的数据绑定到ng-multiselect-dropdown中?

OpenWeather API 中的无

jQuery 触发选择使用 Knockout.js 的站点中数据绑定中的更改值 - 可能吗?

如何将下拉数据绑定添加到 API Json 数据表?

从 openweather api 中提取数据