How to save an array of objects?

I have an object. imageI can do things like image.top, and it will return a value, or I can do image.myPoints[0].leftand it will return a value. I basically have this image object that stores values ​​for me. I want to be able to put multiple objects into an array imageso that I can do something like this:

$("#toPinpoint").mapImage({
                useAjax: false,
                pinpoints: [ { "top": 50,
                           "left": 280,
                           "width": 200,
                           "height": 200},
                         { "top": 0,
                           "left": 0,
                           "width": 300,
                           "height": 74 } ] 
            });

I use this function to create an object, exact points are added to the object. When the mapImage function is called, this happens:

    $.fn.mapImage = function(options){



    //Set user input options and defaults
    var optionConfig = $.extend({}, $.fn.mapImage.defaults, options);

    image=this;
    this.image = this;

    // Assign defaults
    this.previewMode = optionConfig.previewMode;
    this.pinpoints = optionConfig.pinpoints;
    image.pinpoints = optionConfig.pinpoints;
    image.pinpointCount = 0;
    image.selected = 0;
    ...}

This sets the image properties, and now I want to change the properties with my application, and then SAVE these objects imageinto an array.

, , , , , , . , myArray[0].myPoints[0].left, , 30, , myPoints [0].left, 20, , , 20 30. , . !

+3
3

array.push(x) x array. x, , x push(), x - , . ( top image push() ing, image push()).

+4
var myArray = new Array();

"".

0

, , .

(function($){
  $.fn.mapImage = function(options) {
    var defaults = { /* your defaults here */ };

    // Merge default options with passed in options and store
    options = ($.extend({}, defaults, options));

    // Do other stuff
  };
})(jQuery);

// Backup current pinpoints and modify the current one
$('#toPinpoints').data('pinpoints-backup', $('#toPinpoints').data('pinpoints').splice());
$('#toPinpoints').data('pinpoints')[0].top = 100;

// Push a new array of pinpoints
$('#toPinpoints').data('pinpoints').push({"top": 100, "left": 50, "width": 200, "height": 150});

jQuery.extend(), , , , , , .

If you need to access point data, you can use it later by jQuery.data()doing something like this.data($.extend({}, $.fn.mapImage.defaults, options));instead of reassigning the options variable. Points for this element can then be accessed anywhere on $('#toPinpoint').data('pinpoints'), which will return the same array that you passed, using the options to mapImage(). Then you can cross all points with jQuery.each().

If I am missing something in what you are trying to do, let me know.

0
source

Source: https://habr.com/ru/post/1792431/


All Articles