data do not parse json! #610

Closed
codanator opened this Issue Oct 10, 2012 · 15 comments

Comments

Projects
None yet
4 participants

in the documentation by the data() function you write:

• JSON values are parsed, if it’s valid JSON;

if i try things like this

data-plugin-options='{"message":"Goodbye World!"}'

the data function returns a string not a object!

Collaborator

mislav commented Oct 10, 2012

Are you using the latest release? This feature is only present in edge version from master branch. I forgot to state this in the documentation, sorry :(

There will be a 1.0 release soon. In the meantime, get the edge version from master, and build your copy of Zepto

thanks!

@codanator codanator closed this Oct 11, 2012

maybe ,
in zepto.js line 978 ,
jsonType = 'application/json', ----->
jsonType = 'application/json, text/javascript, /; q=0.01',

in jquery/1.7.2

            // Set the Accepts header for the server, depending on the dataType
    jqXHR.setRequestHeader(
        "Accept",
        s.dataTypes[ 0 ] && s.accepts[ s.dataTypes[0] ] ?
            s.accepts[ s.dataTypes[0] ] + ( s.dataTypes[ 0 ] !== "*" ? ", " + allTypes + "; q=0.01" : "" ) :
            s.accepts[ "*" ]
    );

findorf commented Dec 19, 2012

Even if I get the latest, Zepto still returns the object as a string.

In line 21 of the test/data.html file, I write this

<div id="data_obj" data-mode="awesome" data-plugin-options='{"message":"Goodbye World!"}'></div>

and with Zepto I do this

$('#data_obj').data()

It returns
answer: 42 color: "blue" foo: "baz" mode: "awesome" pluginOptions : "{"message":"Goodbye World!"}"

Collaborator

mislav commented Dec 19, 2012

Are you using the latest from GitHub master branch?

findorf commented Dec 19, 2012

Yes, I just downloaded the master branch.

Collaborator

mislav commented Dec 19, 2012

I get parsed JSON when I try this. Which browser are you using?

findorf commented Dec 19, 2012

Firefox 17.0.1 but my chrome does the same...
It works if I set the object from the console and reads it after that. But reading the object from the html dont.

Collaborator

mislav commented Dec 19, 2012

Can you make a failing test for me? For instance, this is an existing JSON attribute test from zepto.html. Does it fail?

      testDataJsonType: function(t){
        var el = $('<div data-json=\'["one", "two"]\' data-invalid=\'[boom]\' />')
        var json = el.data('json')
        t.assertEqual(2, json.length)
        t.assertEqual("one", json[0])
        t.assertEqual("two", json[1])
        t.assertEqual('[boom]', el.data('invalid'))
      },

findorf commented Dec 19, 2012

Well, that one works fine.

BUT:

Instead of writing it to the dom and then read it again.

Try adding data-json='["one", "two"]' to the '#data_obj' in the 'test/data.html' file - and then read the data from the dom (without writing it with script).

Collaborator

mislav commented Dec 19, 2012

I did and I cannot reproduce. You'll have to submit me a failing test to prove there is a bug

findorf commented Dec 19, 2012

JS test

testInitialDataAsObject: function(t) {
        var el = $('#data_obj')

        var jsonObj = el.data('json');
        var pluginOptions = el.data('pluginOptions');

        t.assertEqual(typeof jsonObj, typeof ["one", "two"]);
        t.assertEqual(typeof pluginOptions, typeof {"message":"Goodbye World!"});
      },

And the required HTML

<div id="fixtures">
  <div id="data_attr" data-one="uno" data-two="due" data-foo-bar="baz"></div>
  <div id="data_full" data-mode="awesome"></div>
  <div id="data_obj" data-mode="awesome" data-plugin-options='{"message":"Goodbye World!"}' data-json='["one", "two"]' ></div>
  <ol id="data_list">
    <li data-category="arts"></li>
    <li data-category="science"></li>
  </ol>
  <ol id="data_list2">
    <li></li>
    <li></li>
  </ol>
  </div>
Collaborator

mislav commented Dec 19, 2012

Your test passes for me in multiple browsers. I've also improved it a little bit, but still passes:

      testInitialDataAsObject: function(t) {
        var el = $('#data_obj')

        var jsonObj = el.data('json')
        var pluginOptions = el.data('pluginOptions')

        t.assertEqual('one,two', jsonObj.join(','))
        t.assertEqual("Goodbye World!", pluginOptions.message)
      },

Are you sure you have the latest version?

git remote -v
git fetch origin
git stash # saves your changes
git reset --hard origin/master
git stash pop # restores your changes

findorf commented Dec 19, 2012

Sorry for wasting your time. Seems like getting latest in github is not within the scope of a frontend developer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment