2016-11-10 28 views
1

您可以指定/更改data-dojo-attach-point的範圍爲當前小部件以外的其他部分嗎?Dojo更改數據範圍dojo附加點

例如。我有一個名爲parent的模板化小部件。在那個模板中我有另一個叫做child1的小部件。嵌套在child1中,我有一些小部件。我想要將這些嵌套的小部件綁定到child1而不是父級。

編輯:

<div data-dojo-type="someContainer" data-dojo-attach-point="parent"> 
    <div data-dojo-type="somePane" data-dojo-attach-point="child1"> 
     <span data-dojo-attach-point="(I want to be bound to somePane)"></span> 
    </div> 
</div> 

我想綁定的 「跨度」,以somePane,而無需經過someContainer。

+0

你把所有的人放在一個單一的模板 - 父?通過將嵌套的小部件綁定到child1而不是父級,您的意思是什麼? – erotavlas

+0

是的,一切都在一個單一的模板。通過數據綁定到child1我的意思是指向child1的data-dojo-attach-point而不是父級,這是默認的 – user7142947

+0

據我所知,模板的附加點總是屬於它的相關小部件,而不屬於你的子部件附加到它。所以從小孩開始,您必須獲得對父控件的引用才能訪問父模板中定義的附加點。但你想做什麼?你應該在帖子中提供更多細節,也許有更好的設計可以實現相同的目標。 – erotavlas

回答

0

將跨度分成它自己的小部件,您可以像這樣將它們添加到父項。

包含內容窗格

<div style="width: 100%; height: 100%;"> 
    <div data-dojo-type="dijit/layout/LayoutContainer" style="width: 100%; height: 100%" data-dojo-attach-point="mainNode"> 
     <div data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'top'" > 
      <div > 
       <!--some content--> 
      </div> 
     </div> 

     <div data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'" id="center"> 
      <div data-dojo-attach-point="centerNode"></div> 
     </div> 

    </div> 

</div> 

在你的父窗口部件的postCreate方法創建子的新實例,並將其連接到父

父模板

define([ 
    "dojo/_base/declare", 
    "dijit/_WidgetBase", 
    "dijit/_TemplatedMixin", 
    "dijit/_WidgetsInTemplateMixin", 

    "dojo/text!./templates/Parent.html", 

    "somePath/childWidget", 

    'dojo/domReady!' 
], function (
    declare, 
    _WidgetBase, 
    _TemplatedMixin, 
    _WidgetsInTemplateMixin, 

    parentTemplate, 

    ChildWidget 

) { 

    return declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], { 

     templateString: parentTemplate, 


     postCreate: function() { 
      this.inherited(arguments); 

      var myChild = new ChildWidget(); 
      myChild.placeAt(this.centerNode); 
      myChild.startup(); 

     } 


    }); 
}); 

則由於跨度爲它自己的小窗口裏,你可能會爲它看起來像這樣

<div> 
    <span data-dojo-attach-point="spanNode"></span> 
</div> 

所以,現在的跨度連接點從母公司分離有一個模板。您可以直接參考您爲跨度創建的小部件中的'spanNode'。

聲明的方式, 在childWidget包含的跨度,你可以給給它一個類名像這樣

return declare("childWidget", [_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], { ... 

而在父模板,而不是使用一個連接點來連接部件使用的數據道場型這樣的

<div data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'" id="center"> 
     <div data-dojo-type="childWidget"><!--child widget will get attached here--></div> 
    </div> 
+0

這有效,但我一直在尋找一個聲明性解決方案,因爲我想讓其他人能夠在您的示例中,在「centerNode」本身內的任何位置添加childWidget。可能是第一個元素,可能是最後一個,可以嵌套2個層次,等等...... – user7142947

+0

@ user7142947誰是其他人?用戶還是程序員?你想用這個應用程序的特定部分來完成什麼?如果您從用戶的角度提供更多詳細信息,這可以得出更好的迴應。 – erotavlas

+0

@ user7142947添加了聲明式的方式來結束髮帖 – erotavlas