简体   繁体   English

Javascript-Dojo-自身的对象引用

[英]Javascript - Dojo - Object reference to itself

I am using the Dojo JS framework 1.6 to declare and track custom classes. 我正在使用Dojo JS框架1.6来声明和跟踪自定义类。 I want to use these classes to create reusable functionality such as user edit dialogue box and so on.. 我想使用这些类来创建可重用的功能,例如用户编辑对话框等。

The problem is however when using a method inside the class to create a dojo html type button for example. 但是,问题出在使用类内部的方法创建例如dojo html类型按钮时。 If that button then needs to call a method back within the class, it doesn't know the instantiated variable to call.. 如果该按钮随后需要在类内调用方法,则它不知道要实例化的变量。

How would i get stage2 to reference this instance of the class without hard coding the object name? 我如何在不对对象名称进行硬编码的情况下让stage2引用该类的实例?

Example Class: 示例类:

dojo.provide('edit.contacts');
dojo._hasResource["edit.contacts"] = true;

dojo.declare("edit.contacts", null,
{
   /*
   *     Init
   */
   init   : function(customer_id)
   {
      var out = ''
      +'<button dojoType="dijit.form.Button" onClick="stage2();" />Edit</button>'
      +'';

      // Create the dlg box
      var edit_contacts_dlg = new dijit.Dialog(
      {
         title    : 'New Diag',
         style    : 'width:550px; height:600px;background:#FFFFFF;',
         id       : 'edit_contacts_dlg',
         content  : out
      }).show();
   },

   /*
   *     Stage 2
   */
   stage2   :  function()
   {
      alert('halllo');
   }
}

Example Usage: 用法示例:

It's a problem of scope. 这是范围的问题。 You will always face this kind of problem when mixing JS and HTML to create nodes or widgets. 当混合JS和HTML来创建节点或小部件时,您将始终会遇到此类问题。 You need a reference to the instance of the button widget in the moment you're calling init() method, which you don't have because you used HTML and dojo.parser to create the button. 在调用init()方法时,您需要引用按钮小部件的实例,而您没有该引用,因为您使用HTML和dojo.parser创建了按钮。 Basically, you have three ways out: 基本上,您有三种解决方法:

  1. Create the button programmatically and give it the onClick either directly or via a dojo.connect. 以编程方式创建按钮,然后直接或通过dojo.connect将其赋予onClick。 This is very flexible since you can literally pass any function, using any variables in scope, to the onclick. 这非常灵活,因为您可以使用范围内的任何变量将任何函数从字面上传递给onclick。

     var out = new dijit.form.Button({ label: 'Edit', onClick: dojo.hitch(this, 'stage2') }); var edit_contacts_dlg = new dijit.Dialog({ title : 'New Diag', style : 'width:550px; height:600px;background:#FFFFFF;', id : 'edit_contacts_dlg_' + uniqueId, content : out }).show(); 
  2. Generate unique id for each instance of edit.contants class and insert it into your html string. 为每个edit.contants类实例生成唯一的ID,并将其插入到html字符串中。 Then obtain the instance of the button via dijit.byId() and do the above mentioned connect: 然后通过dijit.byId()获得按钮的实例,并执行上述连接:

     var out = '<button dojoType="dijit.form.Button" id="' + uniqueId + '">Edit</button>'; var edit_contacts_dlg = new dijit.Dialog({ title : 'New Diag', style : 'width:550px; height:600px;background:#FFFFFF;', id : 'edit_contacts_dlg_' + uniqueId, content : out }).show(); var btn = dijit.byId(uniqueId); dojo.connect(btn, "onClick", this, "stage2"); 
  3. Create class based on dijit._Widget and dijit._Templated, ie widget, and use its data-dojo-attach-point feature to obtain button instance ( see it in action at jsFiddle ): 创建基于dijit._Widget和dijit._Templated(即小部件)的类,并使用其data-dojo-attach-point功能获取按钮实例( 请参见jsFiddle上的操作 ):

     dojo.require("dijit._Widget"); dojo.require("dijit._Templated"); dojo.require("dijit.Dialog"); dojo.require("dijit.form.Button"); dojo.ready(function() { dojo.declare("edit.ContactTemplate", [dijit._Widget, dijit._Templated], { templateString: '<div><button data-dojo-type="dijit.form.Button" data-dojo-attach-point="editBtn">Edit</button></div>', widgetsInTemplate: true }) dojo.declare("edit.contacts", null, { init: function(customerId) { this.customerId = customerId; var widget = new edit.ContactTemplate(); dojo.connect(widget.editBtn, "onClick", this, "stage2"); this.editContactDlg = new dijit.Dialog({ title : "Dialog: " + customerId, style : "width:200px;height:80px;background:#FFFFFF;", id : "edit_contacts_dlg_" + customerId, content : widget }); this.editContactDlg.show(); return this; }, stage2: function() { alert(this.customerId); } }); var c1 = new edit.contacts().init("customer #1"); var c2 = new edit.contacts().init("customer #2"); }); 

This is useful when you need a reference to many widgets/nodes. 当您需要引用许多小部件/节点时,这很有用。

I don't really use Dojo, but I tried out a few things. 我不是真的使用Dojo,但是我尝试了一些方法。 It looks like you can use This just as you would normally. 看起来您可以像平常一样使用This。 Try out this demo: http://jsfiddle.net/dSpsr/11/ 试试这个演示: http : //jsfiddle.net/dSpsr/11/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM