缓存! saveLocations()和addLocation()函数有什么区别?

时间:2016-05-14 09:29:50

标签: javascript caching local-storage forecasting weather

我是Javascript的初学者,我接受了一项任务。它基本上是一个天气应用.. 我创建了一个页面,可以根据Google地理定位API输出地点的纬度,经度和昵称。现在我想打电话给forecast.io给我回复结果。 我应该做的是将纬度,经度和昵称存储在LocalStorage中...当"保存位置"单击按钮并将所有位置保存到列表中,以便他们可以单击并获取天气信息。 但后来我给出了一个骨架代码,我不知道它的作用。 this.AddLocation和我在后面写的savelocation()函数有什么区别。 我在这里写的唯一函数是savelocation()函数,它将位置保存到本地存储。其他函数是需要填充的框架代码。

对类中的方法应该做什么的任何解释都会有很大的帮助!

代码如下:

// Returns a date in the format "YYYY-MM-DD".
Date.prototype.simpleDateString = function() {
    function pad(value)
    {
        return ("0" + value).slice(-2);
    }

    var dateString = this.getFullYear() + "-" + 
            pad(this.getMonth() + 1, 2) + '-' + 
            pad(this.getDate(), 2);

    return dateString;
}

// Date format required by forecast.io API.
// We always represent a date with a time of midday,
// so our choice of day isn't susceptible to time zone errors.
Date.prototype.forecastDateString = function() {
    return this.simpleDateString() + "T12:00:00";
}


// Code for LocationWeatherCache class and other shared code.

// Prefix to use for Local Storage.  You may change this.
var APP_PREFIX = "weatherApp";

function LocationWeatherCache()
{
    // Private attributes:

    var locations = [];
    var callbacks = {};

    // Public methods:

    // Returns the number of locations stored in the cache.
    //
    this.length = function() {
    };

    // Returns the location object for a given index.
    // Indexes begin at zero.
    //
    this.locationAtIndex = function(index) {
    };

    // Given a latitude, longitude and nickname, this method saves a 
    // new location into the cache.  It will have an empty 'forecasts'
    // property.  Returns the index of the added location.
    //
    this.addLocation = function(latitude, longitude, nickname)
    {
    }

    // Removes the saved location at the given index.
    // 
    this.removeLocationAtIndex = function(index)
    {
    }

    // This method is used by JSON.stringify() to serialise this class.
    // Note that the callbacks attribute is only meaningful while there 
    // are active web service requests and so doesn't need to be saved.
    //
    this.toJSON = function() {
    };

    // Given a public-data-only version of the class (such as from
    // local storage), this method will initialise the current
    // instance to match that version.
    //
    this.initialiseFromPDO = function(locationWeatherCachePDO) {
    };

    // Request weather for the location at the given index for the
    // specified date.  'date' should be JavaScript Date instance.
    //
    // This method doesn't return anything, but rather calls the 
    // callback function when the weather object is available. This
    // might be immediately or after some indeterminate amount of time.
    // The callback function should have two parameters.  The first
    // will be the index of the location and the second will be the 
    // weather object for that location.
    // 
    this.getWeatherAtIndexForDate = function(index, date, callback) {
    };

    // This is a callback function passed to forecast.io API calls.
    // This will be called via JSONP when the API call is loaded.
    //
    // This should invoke the recorded callback function for that
    // weather request.
    //
    this.weatherResponse = function(response) {
    };

    // Private methods:

    // Given a latitude and longitude, this method looks through all
    // the stored locations and returns the index of the location with
    // matching latitude and longitude if one exists, otherwise it
    // returns -1.
    //
    function indexForLocation(latitude, longitude)
    {
    }
}

// Restore the singleton locationWeatherCache from Local Storage.
//
function loadLocations()
{
}

// Save the singleton locationWeatherCache to Local Storage.
//
function saveLocations(nickname,latitude,longtitude){
 var locations = JSON.parse(localStorage.getItem('APP_PREFIX')) || [];
    locations.push({nickname: nickname, latitude: latitude, longtitude:longtitude});
    localStorage.setItem('APP_PREFIX', JSON.stringify(locations));
}

2 个答案:

答案 0 :(得分:1)

作为本单位的讲师,我建议Stack Overflow不是在你的作业上提问的最佳地点。您的问题的答案需要来自分配说明的知识,这些知识仅适用于参加该单元的学生。

此外,您不应公开发布任何代码(即您的问题解决方案)。作为提交作业的一部分,您签署一份声明,说这是您自己的作品,并且您还没有与任何人分享您的作品。将代码发布到Stack Overflow会打破这个问题。不要这样做!

我建议您仔细阅读作业说明和作业常见问题解答。如果您仍有疑问,请在单位论坛上询问,询问您的示威者,或在咨询或举行桌面会议时询问。

在回答您的问题时,saveLocations()应将LocationWeatherCache实例保存到本地存储。 addLocation()方法应该为locations类的LocationWeatherCache数组属性添加新位置,并且(如HotGirlInYourPracDoingENG1003所示)应该调用saveLocations()以确保此更改保持不变。

答案 1 :(得分:0)

this.addLocation将位置对象添加到var locations。它还应调用saveLocations()将这些更改保存到localStorage