简体   繁体   English

无法在数组中正确保存对象

[英]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: 目标:

  • The program should contain a undo/redo history of a single image 该程序应包含单个图像的撤消/重做历史记录
  • When an image is saved, a new item is pushed on top of the undo/redo history 保存图像后,将在撤消/重做历史顶部添加一个新项
  • When undo is called, the previous image is used instead of the latest 调用undo时,将使用上一个图像而不是最新图像
  • When image is saved again the undo/redo history is truncated so that the latest saved image replaces all the images that were saved after the last undo step. 再次保存图像时,撤消/重做历史记录将被截断,以使最新保存的图像替换上一个撤消步骤之后保存的所有图像。

Problem: 问题:

  • You are wondering why saving an image truncates the redo history and what to do about it 您想知道为什么保存图像会截断重做历史及其处理方法

Answer: 回答:

  • Saving a new image truncates the history and thus mirrors only the latest image 保存新图像会截断历史记录,因此仅镜像最新图像
  • You have to use a redo to select the next step instead of save image or if you want the image to be saved to top of the history then get rid of the splice. 您必须使用重做来选择下一步,而不是保存图像,或者如果您希望将图像保存到历史记录的顶部,那么就可以删除拼接。

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

You can play with with this fiddle. 您可以和这个小提琴一起玩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM