2016-05-14 18 views
-2

Ich bin ein Anfänger in Javascript und ich bekam eine Aufgabe. Es ist im Grunde eine Wetter App. Ich habe eine Seite erstellt, die den Breitengrad, die Länge und den Spitznamen eines Ortes basierend auf Google Geolocation API ausgeben wird. Und jetzt möchte ich provide.io anrufen, um mir die Ergebnisse zurückzugeben. Was ich tun soll, ist die Speicherung der Breiten-, Längen- und Spitznamen im LocalStorage ... wenn die Schaltfläche "Speicherort speichern" angeklickt wird und alle Orte in einer Liste gespeichert werden, damit sie KLICKEN und die Wetterinformationen abrufen können. Aber dann bekomme ich einen Skelettcode, von dem ich keine Ahnung habe, was er macht. Was ist der Unterschied zwischen der This.AddLocation und der Savelocation() Funktion, die ich auf der Rückseite geschrieben habe. Die einzige Funktion, die ich hier geschrieben habe, ist die Funktion savelocation(), die den Speicherort im lokalen Speicher speichert. Die anderen Funktionen sind der Skelett-Code, der gefüllt werden muss.Caches! Was ist der Unterschied zwischen der Funktion saveLocations() und addLocation()?

Jede Erklärung dessen, was die Methoden in der Klasse tun sollten, würde viel helfen!

Der Code ist wie folgt:

// 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)); 
} 

Antwort

0

this.addLocation fügt ein Standortobjekt zu var locations. Es sollte auch saveLocations() anrufen, um diese Änderungen zu localStorage zu speichern.

1

Als Dozent für diese Einheit werde ich vorschlagen, dass Stack Overflow nicht der beste Ort ist, um Fragen zu Ihrer Aufgabe zu stellen. Die Antwort auf Ihre Frage erfordert Wissen aus den Anweisungen zur Aufgabe, die nur den Schülern zur Verfügung stehen, die das Gerät benutzen.

Auch sollten Sie keine Ihrer Code (d. H. Ihre Lösung für das Problem) öffentlich veröffentlichen. Als Teil der Aufgabe unterschreibst du eine Aussage, dass es deine eigene Arbeit ist und du deine Arbeit mit niemandem geteilt hast. Wenn Sie Ihren Code in Stack Overflow eingeben, wird dies abgebrochen. Tu es nicht!

Ich empfehle Ihnen, lesen Sie die Zuordnung Anweisungen und die Zuordnung FAQ. Wenn Sie noch Fragen haben, fragen Sie im Forum der Einheit, fragen Sie Ihren Demonstrator oder fragen Sie in einer Besprechung oder Sitzung.

Als Antwort auf Ihre Frage sollte saveLocations() die LocationWeatherCache Instanz auf lokalen Speicher speichern. Die addLocation() Methode sollte dem locations Array-Attribut der LocationWeatherCache Klasse einen neuen Speicherort hinzufügen und (wie HotGirlInYourPracDoingENG1003 sagt) sollte saveLocations() aufrufen, um sicherzustellen, dass diese Änderung beibehalten wird.