Multiple JSON Sources and Timing in the Drupal Setting
Hi,
I've been really enjoying implementing this easy accordion script lately in my Drupal themes, due to its ease of use and modification. Typically, I use a Drupal Datasource view, which pulls content from the database and converts it into nicely clean JSON. However, I've run into some issues this time due to having multiple JSON sources. I can't seem to initiate accordion after all three sources have loaded. As a easyAccordion() gets called several times, jacking everything up. It's important for me to stick with Drupal 6 on this one, which means JQuery 1.2.6 (I also can't use the JQuery update module on this client). Any suggestions as to making sure all three sources have loaded before I call easyAccordion()? Thanks.
Here is a JSON sample:
- {"nodes":[{
- "node":{
- "title":"Board Room & Lounge",
- "image":"http:\/sites\/default\/files\/imagecache\/390x\/OMF_boardrm_1.JPG"}
- }
- ]}
Here is my JS that implements the plugin:
- var OmfEasyAccordion = OmfEasyAccordion || {};
- Drupal.behaviors.OmfEasyAccordion = function(context) {
-
- if ($('body').attr('class').search('page-new-accordion-page') <= 0) {
- return;
- }
- /**
- * set some pararameters for operation including a map of sources.
- * sources map: list of titled resources, each will be converted into a slide. Each should have a primary source json object.
- * If an alternate is provided the it will fill the closed blade state. Otherwise just the title or number is used.
- */
- OmfEasyAccordion.params = {
- 'animate' : false,
- 'numberedBlades' : false,
- 'target' : '#node-8903 #target',
- 'sources' : {
- 'services' : {
- 'primary' : '/services/json',
- 'alternate' : ''
- },
- 'education' : {
- 'primary' : '/education/json',
- 'alternate' : ''
- },
- 'tools' : {
- 'primary' : '/tools/json',
- 'alternate' : ''
- }
- },
- }
-
- OmfEasyAccordion.build(data = null);
- var count = 1;
- var totalLength = $.param(OmfEasyAccordion.params.sources).split('=').length-1;
-
- $.each(OmfEasyAccordion.params.sources,
- function (i, val) {
- count ++;
- $.getJSON(val.primary,
- function(data, result) {
- dt = '<dt>' + i + '</dt>';
- OmfEasyAccordion.build(dt);
- dd = '<dd>';
- $.each(data.nodes,
- function(z,item) {
- $.each(item.node,
- function(t, field) {
- if (OmfEasyAccordion.formatTypes(t, field)) {
- dd += OmfEasyAccordion.formatTypes(t, field);
-
- }
- }
- );
-
- dd += '</dd>';
-
- OmfEasyAccordion.build(dd);
-
- if (count == $.param(OmfEasyAccordion.params.sources).split('=').length) {
- alert(count);
- alert($.param(OmfEasyAccordion.params.sources).split('=').length);
- $('#easy-accordion').easyAccordion(
- {
- slideNum: false,
- autoStart: true,
- slideInterval: 3000
- }
- );
- }
- }
- );
- }
- );
- }
- );
- }
- OmfEasyAccordion.formatTypes = function(type, data) {
- switch (type) {
- case 'image':
- return '<img src="' + data + '" />';
- break;
- case 'title':
- return '<h3>' + data + '</h3>';
- break;
- case 'body':
- return '<p>' + data + '</p>';
- break;
- }
-
- }
- OmfEasyAccordion.build = function(data) {
- if (!data) {
- $(OmfEasyAccordion.params.target).prepend('<dl id="easy-accordion"></dl>');
- } else {
- $('#easy-accordion').append(data);
- }
- }