开发者

Passing value through JQuery options object

I'm having a problem understanding how callback functions work.

I'm using JCrop to crop many images on a page. JCrop is written under the impression that there may only be 1 image that needs to be cropped:

jQuery(function() {

              CropMe.Jcrop //CropMe is the class holding the image
              ({
                  aspectRatio: 1,
                  onSelect: updateCoords
              });
          }
      });

When it updates the coordinates via "onSelect" it outputs the coords in the function "updateCoords " so that they can be read in a form later on submit:

function updateCoords(c) {

$('.x').val(c.x);

$('.y').val(c.y);

$('.w').val(c.w);

$('.h').val(c.h); }

the problem is, is that I have MANY Jcrops instantiated under a class, not a specific id. Therefore, when updateCoords is called, it doesn't know which x,y,w,h values to update.

Ho开发者_C百科w would I pass an argument(specifically CropMe) through the option object so that i can change the 4 relevant values

relevant code:jquery.jcrop,js


          CropMe.Jcrop //CropMe is the class holding the image
          ({
              aspectRatio: 1,
              onSelect: function(c) { updateCoords(c, CropMe); }
          });

Then updateCoords would look something like:

function updateCoords(c, cropMeObject) {
  // cropMeObject is the CropMe object
}


If you want the this keyword to refer to the CropMe object using a closure should work:

CropMe.Jcrop //CropMe is the class holding the image
  ({
      aspectRatio: 1,
      onSelect:    (function(c) { return updateCoords(c); })(c,this);
  });     

now in function updateCoords you can refer to this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜