Javascript回调修改其所属的对象

发布于 2024-11-27 15:58:39 字数 782 浏览 0 评论 0原文

我正在尝试从另一个具有“照片”列表(图库)的对象执行在对象(照片)中声明的回调,我希望该回调函数能够修改它所属的确切对象。我有这样的事情:

var photo= function(){
 this.Id;
 this.Url;
 this.someCallBack = function(event, ui, object){
  this.Id = object.Id;
  this.Url = object.Url;
 }
}

var gallery = function(){
 this.Name;
 this.PhotoList = new Array();
 this.Draw = function(){
  for(var i =0; i < this.PhotoList.length; i++){
   var self = this;
   $('#'+this.PhotoList[i].Id).droppable({drop:function(event, ui){
    self.PhotoList[i].someCallBack(event, ui, new{Id = 15, Url = 'someUrl.jpg'});
   }
   });
  }
 }
}

事件是 drop (来自 jquery ui droppable),我尝试过但它不起作用,有一个“无法调用未定义的方法'someCallBack'”错误。

目标是在每次回调中修改“库”中列表中的原始对象,以便我可以在完成拖动后获取列表并保存它。

有没有办法实现我之前描述的行为?

我将不胜感激任何帮助。

谢谢

I'm trying to execute a callback that is declared in an object (Photo), from another object that has a list of "Photos" (Gallery) I want that callback function to modify the exact object where it belongs. I have something like this:

var photo= function(){
 this.Id;
 this.Url;
 this.someCallBack = function(event, ui, object){
  this.Id = object.Id;
  this.Url = object.Url;
 }
}

var gallery = function(){
 this.Name;
 this.PhotoList = new Array();
 this.Draw = function(){
  for(var i =0; i < this.PhotoList.length; i++){
   var self = this;
   $('#'+this.PhotoList[i].Id).droppable({drop:function(event, ui){
    self.PhotoList[i].someCallBack(event, ui, new{Id = 15, Url = 'someUrl.jpg'});
   }
   });
  }
 }
}

The event is drop (from jquery ui droppable), I tried but it does't work, there is an "cannot call method 'someCallBack' of undefined" error.

The objetive is to modify the original objects from the list in "gallery" in every callback so I can take the list after I've finished the dragging and save it.

Is there a way to achieve the behavior I described before??

I'd appreciate any help.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时间你老了 2024-12-04 15:58:39

除了 photo 函数中 someCallback 声明无效外,您可以尝试使用 $('#somePhoto').data('the-photo-object', photo)存储您的照片对象,并使用 $('#somePhoto').data('the-photo-object')drop 中的 jQuery 元素检索您的照片对象code> 事件回调会让你省去一些麻烦。

而不是这样

{drop:function(event, ui){
    self.PhotoList[i].someCallBack(event, ui, new{Id = 15, Url = 'someUrl.jpg'});
}}

会更容易混淆:

// creating a new photo object and assigning it to a jquery element
var aPhoto = new photo();
$('#draggable-photo').data('the-photo-object', aPhoto);

// retrieving photo objects from the dom that is being dragged.
{drop:function(event, ui){
   var droppable = $(this);
   var draggable = ui.draggable;
   // assuming draggable is photo, droppable is gallery
   var photo = draggable.data('the-photo-object');
   photo.someCallBack(event, ui, new{Id = 15, Url = 'someUrl.jpg'});
}}

Apart from the invalid declaration of someCallback in photo function, you can try to use $('#somePhoto').data('the-photo-object', photo) to store your photo object, and use $('#somePhoto').data('the-photo-object') to retrieve your photo object from a jQuery element in your drop event callback will save you some headache.

Instead of

{drop:function(event, ui){
    self.PhotoList[i].someCallBack(event, ui, new{Id = 15, Url = 'someUrl.jpg'});
}}

This would be less confusing:

// creating a new photo object and assigning it to a jquery element
var aPhoto = new photo();
$('#draggable-photo').data('the-photo-object', aPhoto);

// retrieving photo objects from the dom that is being dragged.
{drop:function(event, ui){
   var droppable = $(this);
   var draggable = ui.draggable;
   // assuming draggable is photo, droppable is gallery
   var photo = draggable.data('the-photo-object');
   photo.someCallBack(event, ui, new{Id = 15, Url = 'someUrl.jpg'});
}}
唱一曲作罢 2024-12-04 15:58:39

从这个小代码片段来看,我并没有真正遵循代码流程,也没有看到足够的信息来将其放入 jsFiddle 中,所以我只会评论我看到的错误。

我在代码中没有看到任何实际将 someCallback 函数分配给对象的地方。也许您的意思是这段代码:

var photo= function(){
 this.Id;
 this.Url;
 this.someCallBack(event, ui, object){
    this.Id = object.Id
    this.Url = object.Url
 }
}

实际上将函数附加到对象,删除一些无操作语句,将“object”更改为“obj”,这样就不会对“Object”类产生任何混淆,并添加缺失的半-冒号:

var photo = function(){
    this.someCallBack = function(event, ui, obj){
        this.Id = obj.Id;
        this.Url = obj.Url;
    }
 }

在行尾还缺少很多分号,并且不确定为什么在不使用事件和 ui 时将它们传递给 someCallback 函数。

From this little code snippet, I don't really follow the flow of the code or see enough info to put it into a jsFiddle, so I'll just comment on errors I see.

I don't see any place in your code where you're actually assigning the someCallback function to your object. Perhaps you mean for this code:

var photo= function(){
 this.Id;
 this.Url;
 this.someCallBack(event, ui, object){
    this.Id = object.Id
    this.Url = object.Url
 }
}

to be this which actually attaches the function to the object, removes some no-op statements, change "object" to "obj" so there's never any confusion about the "Object" class and adds missing semi-colons:

var photo = function(){
    this.someCallBack = function(event, ui, obj){
        this.Id = obj.Id;
        this.Url = obj.Url;
    }
 }

Also missing a lot of semi-colons at the end of lines and unsure why you're passing event and ui to the someCallback function when you're not using them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文