[英]Having trouble saving objects properly in array
var currentState = 1;
var mainImage);
$.fn.mapImage.saveImage = function(image) {
if (imageState[currentState] != undefined) {
imageState.splice(currentState - 1);
imageState.push($.extend(true, {}, image));
} else {
imageState.push($.extend(true, {}, image));
}
currentState++;
mainImage = image;
}
$.fn.mapImage.undoImage = function() {
if (currentState != 1) {
currentState--;
mainImage = imageState[currentState - 1];
$.fn.mapImage.loadState(imageState[currentState - 1]);
}
}
$.fn.mapImage.loadState = function(image) {
mainImage = image;
}
If I call saveImage a few times and increase the currentState, then call undoImage a couple times, and then call saveImage again it will go through that if statement inside of saveImage. 如果我多次调用saveImage并增加currentState,则多次调用undoImage,然后再次调用saveImage,它将通过saveImage内部的if语句。 For some reason that imageState that is pushed on in the if statement (inside saveImage) mirrors any image that is pushed on to the array after that, it will just mirror the most recently pushed on one.
由于某种原因,在if语句中(在saveImage内)推入的imageState会镜像此后推入数组的任何图像,它只会镜像最近推入到数组中的图像。 I have a feeling there is a reference problem somewhere, but I cant seem to find it.
我觉得某处存在参考问题,但似乎找不到。
I have not worked with JavaScript OR jQuery extensively, but I will do my best to help you out. 我没有广泛使用JavaScript或jQuery,但我会尽力帮助您。
First, a simple coding error. 首先,一个简单的编码错误。 Line 2 should be: var mainImage;
第2行应为:var mainImage; Remove the extra parenthesis.
删除多余的括号。
In saveImage you check to see if currentState is in use. 在saveImage中,检查是否正在使用currentState。 Then you splice from (currentState-1).
然后,您从(currentState-1)进行拼接。 This may be deleting one too many images?
这可能会删除太多图像? Or you may be checking one too few images?
或者您可能正在检查一张图像太少? I'm not sure, but one of these seems to be the case.
我不确定,但是似乎是其中一种。 Look at the following:
请看以下内容:
$.fn.mapImage.saveImage = function(image) {
if (imageState[currentState-1] != undefined) {
imageState.splice(currentState - 1);
imageState.push($.extend(true, {}, image));
} else {
imageState.push($.extend(true, {}, image));
}
currentState++;
mainImage = image;
}
Finally, undoImage seems to be redundant in setting mainImage and then calling loadState (which does the same thing). 最后,在设置mainImage然后调用loadState(执行相同操作)时,undoImage似乎是多余的。 You should stick with one method of changing mainImage.
您应该坚持使用一种更改mainImage的方法。
I read the question as follows: 我阅读以下问题:
Goal: 目标:
Problem: 问题:
Answer: 回答:
Some modifications: 一些修改:
$.fn.mapImage = function() {
// changed the currentState to start at 0 as you repeated currentState - 1 three times
// while used the currentState as is only once.
var currentState = 0;
var mainImage;
var imageState = [];
this.saveImage = function(image) {
// Truncates the redo history as redos are no longer
// valid. Start a new history using the latest saved
// image.
if (imageState[currentState + 1] != undefined) {
// XXX: splice(index) is not standard, you should use
// splice(index, howMany) instead
imageState.splice(currentState + 1, imageState.length);
}
// The rest of the if branch and the else branch were identical
// No need for the else branch
imageState.push($.extend(true, {}, image));
// It's more consistent to set the main image the same way
// each time by using the loadState function as you already did
// in undo image.
this.loadState(imageState[currentState]);
currentState++;
};
this.undoImage = function() {
// replaced != 1 with > 0 to prevent defects
if (currentState > 0) {
currentState--;
// You set the main image in loadState
// so you don't have to set it here
// mainImage = imageState[currentState];
this.loadState(imageState[currentState]);
}
};
this.redoImage = function() {
if (currentState + 1 < imageState.length) {
currentState++;
this.loadState(imageState[currentState]);
}
};
this.loadState = function(image) {
mainImage = image;
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.