如何将[object Object]传递给JS函数

时间:2014-03-12 02:22:30

标签: javascript jquery tcl

这就是场景。我们使用了预先存在的拖放功能,我将其扩展用于不同的情况。

(1)在dragdrop.js文件中建立了很多局部变量,上下文是 this (this.dragId等)。

(2)我正在使用模态来显示复选框列表。用户选择他想要的数据并单击“保存”。

(3)我调用saveAttribute()并使用JQuery来获取选中的值。但是,没有上下文,我无法继续这个项目的工作。

Without passing *this*, I don't have a context. 
If I try to pass *this* in the first call from JS to a tcl file and then back 
to a JS function, it expects JS to read [object Object]. I m not sure how to do this. 
Any help is appreciated. 

传递给tcl文件之前的 this.toSource(),看起来像这样

({m_pageWrap:{}, m_parentDragSite:{m_pageWrap:{}, m_parentDragSite:null, m_CanHaveChildSites:true, m_groups:[{}, {}], m_allDraggableBoxes:[{m_box:{}, childSite:{m_pageWrap:{}, m_parentDragSite:{}, m_CanHaveChildSites:false, m_groups:[], m_allDraggableBoxes:[{m_box:{}, childSite:null, m_dragSite:{}, m_handle:null, m_coord:{x:0, y:0}, m_handleCoord:{x:0, y:0}, m_handlePositionStale:true, m_positionStale:true, m_cachePosition:true, m_isBeingEdited:false, m_BoxHandleClone:null, m_dragHandleObj:{m_handle:null, m_BoxHandleClone:null, m_isBeingEdited:false, m_OwningDraggableBox:null, m_saveButton:null}, m_commandHandle:null, m_removeCommandHandle:null, m_insertCommandHandle:null, m_reuseables:[], m_commands:[], m_isBeingCreated:false, m_isDummy:true, m_heightToExclude:0, m_saveDeleteIfno:false, m_canBeDropTarget:false}], m_draggedBoxIndex:-1, m_OrigBoxPos:{x:0, y:0}, m_InitialMousePos:{x:0, y:0}, m_Mousediff:{x:0, y:0}, m_CurrentMousePos:{x:0, y:0}, m_BoxClone:null, m_DragPositiveDirection:true, m_Dragging:false, threshold:2, m_Released:false, m_DraggedObject:null, m_DummyBox:{}, m_defaultBoxID:null, m_boxCloneY:0, m_allReuseables:[], m_drag_events:[], m_EventListenerFuncName:"addEventListener", m_EventListenerFuncPrefix:"", m_isDraggableWithinSite:false, m_isDroppableWithinSite:false, m_ignoreSiteHasDroppables:true, m_parentOfBox:{}}, m_dragSite:{}, m_handle:null, m_coord:{x:0, y:0}, m_handleCoord:{x:0, y:0}, m_handlePositionStale:true, m_positionStale:true, m_cachePosition:true, m_isBeingEdited:false, m_BoxHandleClone:null, m_dragHandleObj:{m_handle:null, m_BoxHandleClone:null, m_isBeingEdited:false, m_OwningDraggableBox:null, m_saveButton:null}, m_commandHandle:null, m_removeCommandHandle:null, m_insertCommandHandle:{}, m_reuseables:[], m_commands:[], m_isBeingCreated:false, m_isDummy:false, m_heightToExclude:0, m_saveDeleteIfno:false, m_canBeDropTarget:true}, {m_box:{}, childSite:{}, m_dragSite:{}, m_handle:{}, m_coord:{x:0, y:0}, m_handleCoord:{x:515, y:524}, m_handlePositionStale:true, m_positionStale:true, m_cachePosition:true, m_isBeingEdited:false, m_BoxHandleClone:{}, m_dragHandleObj:{m_handle:{}, m_BoxHandleClone:null, m_isBeingEdited:false, m_OwningDraggableBox:{}, m_saveButton:null}, m_commandHandle:{}, m_removeCommandHandle:{}, m_insertCommandHandle:{}, m_reuseables:[], m_commands:[[{}, "onclick", (function (){return _b3[_b4].apply(_b3,arguments||[]);}), 1], [{}, "onclick", (function (){return _b3[_b4].apply(_b3,arguments||[]);}), 1]], m_isBeingCreated:false, m_isDummy:false, m_heightToExclude:0, m_saveDeleteIfno:false, m_canBeDropTarget:true}], m_draggedBoxIndex:-1, m_OrigBoxPos:{x:0, y:0}, m_InitialMousePos:{x:0, y:0}, m_Mousediff:{x:0, y:0}, m_CurrentMousePos:{x:0, y:0}, m_BoxClone:null, m_DragPositiveDirection:true, m_Dragging:false, threshold:2, m_Released:false, m_DraggedObject:null, m_DummyBox:null, m_defaultBoxID:null, m_boxCloneY:0, m_allReuseables:[], m_drag_events:[], m_EventListenerFuncName:"addEventListener", m_EventListenerFuncPrefix:"", m_isDraggableWithinSite:true, m_isDroppableWithinSite:true, m_ignoreSiteHasDroppables:false, m_parentOfBox:{}, m_formName:"TemplateEditForm"}, m_CanHaveChildSites:false, m_groups:[], m_allDraggableBoxes:[{m_box:{}, childSite:null, m_dragSite:{}, m_handle:null, m_coord:{x:0, y:0}, m_handleCoord:{x:0, y:0}, m_handlePositionStale:true, m_positionStale:true, m_cachePosition:true, m_isBeingEdited:false, m_BoxHandleClone:null, m_dragHandleObj:{m_handle:null, m_BoxHandleClone:null, m_isBeingEdited:false, m_OwningDraggableBox:null, m_saveButton:null}, m_commandHandle:null, m_removeCommandHandle:null, m_insertCommandHandle:null, m_reuseables:[], m_commands:[], m_isBeingCreated:false, m_isDummy:true, m_heightToExclude:0, m_saveDeleteIfno:false, m_canBeDropTarget:true}], m_draggedBoxIndex:-1, m_OrigBoxPos:{x:0, y:0}, m_InitialMousePos:{x:0, y:0}, m_Mousediff:{x:0, y:0}, m_CurrentMousePos:{x:0, y:0}, m_BoxClone:null, m_DragPositiveDirection:true, m_Dragging:false, threshold:2, m_Released:false, m_DraggedObject:null, m_DummyBox:{}, m_defaultBoxID:null, m_boxCloneY:0, m_allReuseables:[], m_drag_events:[], m_EventListenerFuncName:"addEventListener", m_EventListenerFuncPrefix:"", m_isDraggableWithinSite:true, m_isDroppableWithinSite:true, m_ignoreSiteHasDroppables:true, m_parentOfBox:{}})

第一步:JS代码:

a.b.addAttribute = function()
{
try {
    alert(this.toSource()); // You can see the output of this above.
    return modalCheck('/acct/modal-list-attributes?');
    alert("here");
   } catch(err) {       
  }
};

第二步(上下文现在是[object Object],这是在tcl文件中 - 弹出复选框的模式。):

 <input type=\"button\" value=\"[local_string save_text $user_id]\" onclick=\"saveAttribute($context)\" id=\"saveAttr\" class=\"BTNAction\"/> class=\"BTNAction\"/>

这一步 - 我需要帮助将[object Object]传递给在提交按钮中从onclick调用的JS函数。

function saveAttribute(object) {
  alert(object.toSource());
  //some operations to do with help of context.
}

谢谢,

阿南德

3 个答案:

答案 0 :(得分:0)

为了查看对象/变量的值,您不应该使用alert 使用console.log(object);console.info(object);或console.debug(object);`

这将在浏览器开发者控制台上打印值,您可以通过F12

获取

答案 1 :(得分:0)

看起来像经典的JavaScript这个问题。

您如何填写复选框列表?你为此使用某种ajax吗?

答案 2 :(得分:0)

您必须非常清楚解释一段代码的内容。它是客户端Javascript吗?它是服务器端Tcl吗?如果没有这种清晰度,你就会陷入混乱!

最简单的方法是将尽可能多的JS放在静态文件中,以便静态提供;如果你告诉它,托管环境就可以了。然后你只需要在Tcl中生成动态JS的最小位来将各个部分组合在一起;这简单得多,特别是因为你可以使用简单的经验法则来检测危险。例如,您可以说不应该直接使用Tcl命令替换来创建JS,这意味着您可以检查[ ... ]并添加反斜杠而不必考虑太多。 (方括号对两种语言都有意义。$如果你使用jQuery也可能有问题,因为它后面跟着一个字母也是一个Tcl metasyntax字符或括号。多语言工作很糟糕。)

JS this无法传递到服务器并返回;它是非常重要的序列化(并通过任何网络连接强制一些序列化)。传递用于查找this的对象的名称很可能,但您通常不需要这种复杂性。

如果您在Tcl方面有类似this概念,则(可能)可以转移到客户端并返回(即,只要由于Tcl值始终可序列化,因此维护会话上下文。 (不可序列化的部分保持与上下文绑定,即“解释器”。)如果您正在执行此操作,则可能最容易将值放入变量并替换它;即使技术上不需要,也可能更清楚。


您的代码示例对我来说太难以理解,您可以找到一些可以识别相似的示例。 (谁正在处理[object Object]?你使用什么框架?这是一个合法的Tcl命令替换......但我不确切地知道你想用它做什么;我已经没有足够的信息来解决它的语义...)