This is a migrated thread and some comments may be shown as answers.

Need Some Help From A JavaScript Guru

12 Answers 146 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Jeffrey Edgett
Top achievements
Rank 1
Jeffrey Edgett asked on 14 Dec 2011, 10:05 PM
Is there a reason why datasource.total()  returns "undefined" in IE but works okay in Firefox?


12 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 15 Dec 2011, 08:20 AM
Hello Jeffrey Edgett,

Could you please provide a bit more information about your DataSource configuration and scenario?

Regards,
Rosen
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Jeffrey Edgett
Top achievements
Rank 1
answered on 16 Dec 2011, 06:04 PM
I figured out that issue, had to do with the order that the dom is loaded in IE vs. Firefox.   By using the built in "change:" functionality to execute the code that issue was resolved.  

However, now I have another problem.

Data in an XML file that is wrapped in CDATA tags is ignored?  

The JavaScript code:
// === GET RESOURCE FILE / LOCALIZE PAGE ===========================================================================================================
$(document).ready(function () {
    var current_file = "resources" + location.pathname + get_culture_path_modifier() + ".xml";
    var datasource_resource = new kendo.data.DataSource(
    {
        transport:
        {
            read: current_file
        },
        schema:
        {
            type: "xml",
            data: "/root/data",
            model:
            {
                fields:
                {
                    name: "@name",
                    value: "value/text()"
                }
            }
        },
        change: function () {
            if (datasource_resource.total() != undefined) {
                var i = 0;
                for (i = 0; i < datasource_resource.total(); i++) {
                    var item_name = datasource_resource.at(i).name;
                    var item_value = datasource_resource.at(i).value;
                    var element_id = document.getElementById(item_name);
                    if (element_id != null) {
                        var element_type = element_id.tagName.toLowerCase();
                        if (element_type.startsWith('meta') == true) {
                            element_id.setAttribute('content', item_value);
                        }
                        else if (element_type.startsWith('link') == true) {
                            element_id.setAttribute('href', item_value);
                        }
                        else {
                            element_id.textContent = item_value;
                        }
                    }
                }
            }
        }
    });
    datasource_resource.read();
});


A portion of the XML data file:
<data name="title" xml:space="preserve">
        <value>
            My Site
        </value>
    </data>
    <data name="meta_description" xml:space="preserve">
        <value>
            <![CDATA[
                My Site is a template & is under development.
            ]]>
        </value>
    </data>

Any value not in CDATA tags (see title item above) is returned.  

Any value in CDATA tags (see meta_description item above) is returned as an empty string.
0
Rosen
Telerik team
answered on 19 Dec 2011, 10:34 AM
Hi Jeffrey,

The behavior you are experiencing is due to the content in the data attribute. In the sample XML, in the tags with CData tag, there is spaces before and after it, this will create several text nodes, as schema is set to get the last text node from a given tag it will get the last space, thus an empty tag. Therefore, in case with CData you should make sure the CData is the last child text node, for example by removing all of the spaces.

Greetings,
Rosen
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Jeffrey Edgett
Top achievements
Rank 1
answered on 19 Dec 2011, 05:27 PM
Shouldn't it respect the white space as part of the same node, due to xml:space="preserve" being declared?  

I haven't ever encountered this problem in .NET when working with RESX files (the behavior I am trying to mimic).
0
Rosen
Telerik team
answered on 20 Dec 2011, 08:48 AM
Hello Jeffrey Edgett,

I'm afraid that there is not difference if there is space preserve set or not. Please take a look the following jsfiddle which demonstrates how the element is represented after it  been parsed using the browser API:

All the best,
Rosen
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Jeffrey Edgett
Top achievements
Rank 1
answered on 20 Dec 2011, 04:42 PM
Thank you for the example.  

Is there a way to:
  • target the #cdata-section element directly or...

  • remove the surrounding white space so only the element containing content is seen


As an urgent development suggestion / feature request:  Your XML parser is working correctly by displaying each text item is another node.  However, I think that in the future it should honor xml:space="preserve" and treat everying (including the white space) as a single element.


0
Rosen
Telerik team
answered on 20 Dec 2011, 05:31 PM
Hi Jeffrey Edgett,

Straight to your questions:

- I'm afraid not
        - You should format the data before it is feed to the dataSource. For example if it is received from a local service, you should verify that there are not  spaces between the nodes and CData tags.
- Unfortunately, we do not have our own XML parser but use the browser APIs, similar to the sample code from my previous message. Therefore, there is little we can do when the browser parser does not honor the attribute in question.

All the best,
Rosen
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Jeffrey Edgett
Top achievements
Rank 1
answered on 20 Dec 2011, 06:13 PM
Something like this might work?
var xml, tmp,
    data = '<data name="meta_description" xml:space="preserve">' +
        '<value> <![CDATA[My Site is a template & is under development.]]> </value>' +
    '</data>';
try {
    if (window.DOMParser) { // Standard
        tmp = new DOMParser();
        xml = tmp.parseFromString(data, "text/xml");
    } else { // IE
        xml = new ActiveXObject("Microsoft.XMLDOM");
        xml.async = "false";
        xml.loadXML(data);
    }
} catch (e) {
    xml = undefined;
}
 
var element = xml.documentElement.firstChild,
    text = "";
 
for (var node = element.firstChild; node; node = node.nextSibling) {
    if (!(node.nodeType == 3 && node.nodeValue.replace(/^\s+|\s+$/g, '') == '')){
        text += node.nodeValue + "<br />";
    }
}
 
document.getElementById("log").innerHTML = text;
0
Accepted
Rosen
Telerik team
answered on 21 Dec 2011, 10:15 AM
Hi Jeffrey Edgett,

Unfortunately, this approach will have a small drawback. As it skips all empty nodes, the resulting object structure (which is how it is used in the DataSource) will be missing fields/properties. As can be observed in this slightly modified sample:

However, we may consider improving the behavior in a future version of the library.

Greetings,
Rosen
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Jeffrey Edgett
Top achievements
Rank 1
answered on 21 Dec 2011, 05:37 PM
Yes.  
You would probably need to do some additional checks to see if it was the last child element.  
If no value had been found and it was the last child, return the value of the last child, regardless if it is an empty string.

I appreciate you considering this for a future revision!   It definitely is needed by us, as we can not guarantee editors will not add those extra spaces in the file.

0
Jeffrey Edgett
Top achievements
Rank 1
answered on 02 Mar 2012, 10:03 PM
Has there been any further development to correctly handle XML documents with whitespace or carriage returns inside an element.
0
Rosen
Telerik team
answered on 05 Mar 2012, 10:23 AM
Hi Jeffrey,

I'm afraid that there is no change in the behavior discussed in this thread. 

Regards,
Rosen
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Data Source
Asked by
Jeffrey Edgett
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Jeffrey Edgett
Top achievements
Rank 1
Share this question
or