ich für diese um gesucht zu funktionieren, aber so haben bisher nicht gelungen, ein Duplikat zu finden, die ich die falschen Schlüsselwörter werden kann ...Referenz Speicherung in Objekt/Variable
ich vorübergehend bin versucht zu ändern Funktion, die in einem Objekt gespeichert ist, aber Probleme haben, es auf das zurückzusetzen, was es vorher war.
Bedenken Sie:
// Set the options object
var options = {
success: function(){
console.log('Original Function Called');
}
}
// Save the options
$('#foo').data('bar',options);
Und dann in einer anderen Funktion:
// Get the options
var options = $('#foo').data('bar');
// Store the old options
var old_options = options;
// Temporarily change the success function
options.success = function(){
console.log('Temporary Function Called');
}
// Save the options
// This allows the other functions to access the temporary function
$('#foo').data('bar',options);
// Do stuff here that uses the new options
// Reset the options to include the original success function
$('#foo').data('bar',old_options);
Ich würde die Temporary Function Called
nur Anzeige einmal hat jedoch zu erwarten, scheint es völlig den alten success
Rückruf zu ersetzen der temporäre Rückruf.
Kann mir jemand sagen, warum und wie ich das umgehen kann?
UPDATE
Ich dachte, dass extend
dieses Problem beheben würde, aber es scheint, dass das Problem ein wenig tiefer sein kann. Ich habe beschlossen, dieses Mal einen Ausschnitt meines eigentlichen Codes zu posten. Bitte beachten Sie die folgenden vor dem Lesen:
SM
ziemlich viel ist nur ein Alias vonjQuery
, bitte ignorieren.success
underror
sind Parameter an die Funktion geliefert
Hier ist mein Code:
// Get the properties
var properties = $(form).data('autosave');
switch(parameter){
case 'save':
var old_properties = $.extend({},properties);
// Set the new callbacks if they have been supplied
properties.options.success = typeof success!=='undefined' ? success : old_properties.options.success;
properties.options.error = typeof error!=='undefined' ? error : old_properties.options.error;
// Save the properties
$(form).data('autosave',properties);
// Call the save method before setting the interval
SM(form)._as_save();
properties = $.extend({},old_properties);
// Save the old properties
$(form).data('autosave',properties);
// Clear the current interval
clearInterval(properties.interval);
// Call the save method periodically
properties.interval = setInterval(function(){
SM(form)._as_save();
},properties.options.interval);
break;
}
// Save the properties
$(form).data('autosave',properties);
Sie sagen, dass nach '$ ('# foo'). Data ('bar', old_options);' Sie immer noch 'Temporary Function Called' sehen? – crush
@crush Genau! :-) –
@crush Ich habe in der Tat –