2013-04-25 27 views
0

我測試後創造,所以我可以設立postcreate類的屬性,而不是調用這裏如何在Dojo中使用postCreate?

request("js/my/data/sample.json", { 
    handleAs: "json" 
}).then(function (jsonResults) { 
    arrayUtil.forEach(jsonResults.LinksMap, function (List) { 
     arrayUtil.forEach(List.LinksMap.entry, function (Ientry) { 
      if ('information' === Ientry.linkType) Ientry.className = 'info'; 
      else if ('news link' === Ientry.linkType) Ientry.className = 'news'; 
      var widget = new support(Ientry).placeAt(authorContainer); 
     }); 
    }); 
}); 

我試圖

postCreate: function() { 
    this.inherited(arguments); 
}, 
_setLinkClssAttr: function (iconClass) { 
    if (iconClass != "") { 
     if ('information' === linkType) LinkClss = 'info'; 
     if ('news link' === Ientry.linkType) LinkClss = 'news'; 

     this._set("LinkClss", iconClass); 
     this.LinkNode.class = iconClass; 
    } 
} 

我的HTML模板看起來像這樣

<a class="${baseClass}LinkClss" href="${Link.url}" data-dojo-attach-point="LinkNode">${Link.title}</a> 

回答

0

Is this what you mean

HTML代碼:

<div id="myWidget1"></div> 
<div id="myWidget2"></div> 
<div id="myWidget3"></div> 

CSS:

.info { 
    color: red; 
} 
.news { 
    color: blue; 
} 

代碼中創建控件的一個條目中的物體通過:

require([ 
    "dojo/parser", "dojo/dom", "MyWidget", "dojo/domReady!"], 

function (parser, dom, MyWidget) { 
    console.log(arguments); 
    parser.parse().then(function() { 
     var linkTypes = ['information', 'news link', 'other']; 
     linkTypes.forEach(function (linkType, i) { 
      var entry = { 
       link: 'hello' + (i + 1), 
       linkType: linkType, 
       linkHref: "https://www.google.co.uk/search?q=" + linkType 
      }; 
      new MyWidget(entry, dom.byId("myWidget" + (i + 1))); 
     }); 
    }); 
}); 

代碼來定義窗口小部件:

define("MyWidget", [ 
    "dojo/_base/declare", "dojo/dom-class", "dojo/dom-attr", "dojo/query", 
    "dijit/_WidgetBase", "dijit/_TemplatedMixin", "dojo/NodeList-dom"], 

function (declare, domClass, domAttr, query, _WidgetBase, _TemplatedMixin) { 
    var template = [ 
     '<div>', 
     '<a data-dojo-attach-point="linkNode"></a>', 
     '</div>']; 
    return declare("MyWidget", [_WidgetBase, _TemplatedMixin], { 
     templateString: template.join("\n"), 
     constructor: function (params, srcNodeRef) { 
      console.log("creating widget with params " + dojo.toJson(params) + 
       " on node " + srcNodeRef); 
     }, 
     postCreate: function() { 
      console.log(arguments); 
      this.inherited(arguments); 
      this._setLinkClass(); 
     }, 
     // private methods 
     _setLinkClass: function() { 
      var linkClass = this._calculateLinkClass(); 
      console.log(linkClass, this.linkNode); 
      if (linkClass != "") { 
       query(this.linkNode).addClass(linkClass); 
      } 
     }, 
     _calculateLinkClass: function() { 
      var linkClass = ""; 
      if ('information' === this.linkType) linkClass = 'info'; 
      if ('news link' === this.linkType) linkClass = 'news'; 
      return linkClass; 
     }, 
     // Attributes 
     link: "empty", 
     _setLinkAttr: { 
      node: "linkNode", 
      type: "innerHTML" 
     }, 
     linkHref: "#", 
     _setLinkHrefAttr: function (href) { 
      domAttr.set(this.linkNode, "href", href); 
     } 
    }); 
}); 

結果:

enter image description here

+0

保羅,這工作得很好。我做的唯一改變是domClass.add(this.linkNode,linkClass);爲1.8 – bumblebee 2013-04-25 22:35:37