bumblebee
bumblebee

Reputation: 131

How can I use a json member object in dojo widget

I am iterating a json whose format is like this

request("js/my/data/sample.json", {
    handleAs: "json"
}).then(function (jsonResults) {
    arrayUtil.forEach(jsonResults.LinksMap, function (List) {
        arrayUtil.forEach(List.LinksMap.entry, function (Ientry) {
            var widget = new support(Ientry.Link).placeAt(authorContainer);
        });
    });
});

The Widget HTML template looks like

      <div><span  title="${title}">${title}</span></div> <br />
 <div><span title="${description}">${description}</span></div> <br />
 <a class="info" title="${title}" href="${url}">${title}</a><br />

I would like to use the object linkType provided in json to use a different class in the html template of the widget so if it linkType is "information" then use

     <a class="info" title="${title}" href="${url}">${title}</a><br />

If it is news then use

          <a class="news" title="${title}" href="${url}">${title}</a><br />

Upvotes: 1

Views: 113

Answers (1)

Oleg Mikheev
Oleg Mikheev

Reputation: 17444

A combination of this modified template:

<div><span  title="${Link.title}">${Link.title}</span></div> <br />
<div><span title="${Link.description}">${Link.description}</span></div> <br />
<a class="${className}" title="${Link.title}" href="${Link.url}">${Link.title}</a><br />

and this modified code should do the trick:

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);

});});});

But a much better idea would be implementing a postCreate method in your widget that would be able to modify widget in any way you like, after it gets constructed and just before it gets displayed.

Please refer to this guide and search for postCreate there.

Upvotes: 2

Related Questions