Javascript回调修改其所属的对象
我正在尝试从另一个具有“照片”列表(图库)的对象执行在对象(照片)中声明的回调,我希望该回调函数能够修改它所属的确切对象。我有这样的事情:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了 photo 函数中
someCallback
声明无效外,您可以尝试使用$('#somePhoto').data('the-photo-object', photo)
存储您的照片对象,并使用$('#somePhoto').data('the-photo-object')
从drop
中的 jQuery 元素检索您的照片对象code> 事件回调会让你省去一些麻烦。而不是这样
会更容易混淆:
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 yourdrop
event callback will save you some headache.Instead of
This would be less confusing:
从这个小代码片段来看,我并没有真正遵循代码流程,也没有看到足够的信息来将其放入 jsFiddle 中,所以我只会评论我看到的错误。
我在代码中没有看到任何实际将 someCallback 函数分配给对象的地方。也许您的意思是这段代码:
实际上将函数附加到对象,删除一些无操作语句,将“object”更改为“obj”,这样就不会对“Object”类产生任何混淆,并添加缺失的半-冒号:
在行尾还缺少很多分号,并且不确定为什么在不使用事件和 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:
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:
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.