最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Load JSON by URI using inline JSON in Polymer - Stack Overflow

programmeradmin0浏览0评论

I am making a d3.js map ponent with Polymer. The topology data needs to be loaded from a json file and since I don't want the ponent to be dependent on a URL/URI, I am using inline JSON instead of loading it using AJAX:

<polymer-element name="map-us">
    <template>


        <script id="topodata" type="application/json" src="data/us.json"></script> 

        /* ... more stuff ... */

    </template>


    <script>

        Polymer('map-us', {


            ready: function() {

                /* 
                This  works in regular HTML/JQuery:

                var x = JSON.parse($('#myJson').html());
                console.log(x.arcs);

                */

                var x = JSON.parse(this.$.topodata.html());
                console.log(x.arcs);

        });
    </script>
</polymer-element>

But obviously topdata element doesn't have the method html().

Is it the right thing to do? and how vulcanize deal with this?

I am making a d3.js map ponent with Polymer. The topology data needs to be loaded from a json file and since I don't want the ponent to be dependent on a URL/URI, I am using inline JSON instead of loading it using AJAX:

<polymer-element name="map-us">
    <template>


        <script id="topodata" type="application/json" src="data/us.json"></script> 

        /* ... more stuff ... */

    </template>


    <script>

        Polymer('map-us', {


            ready: function() {

                /* 
                This  works in regular HTML/JQuery:

                var x = JSON.parse($('#myJson').html());
                console.log(x.arcs);

                */

                var x = JSON.parse(this.$.topodata.html());
                console.log(x.arcs);

        });
    </script>
</polymer-element>

But obviously topdata element doesn't have the method html().

Is it the right thing to do? and how vulcanize deal with this?

Share Improve this question edited Jun 21, 2015 at 21:10 sepans asked Jul 8, 2014 at 16:18 sepanssepans 1,37213 silver badges24 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

There is no need to inline JSON file. Polymer's resolvePath function is useful here to make sure the relative path to JSON file is always correct regardless of where the ponent is included:

this.topojsonURI = 'data/us.json';

/* d3.json basically just makes the json call */

d3.json(this.resolvePath(this.topojsonURI), function() {
  /* code rendering the map */
}

Info about resolvePath here.

UPDATE (Polymer 1.0):

The function name has been changed to resolveUrl: 1.0 API Docs

You're not depending on an external resource but it's still in a separate file. I'd use core-ajax to load it:

<core-ajax id="topodata" auto url="data/us.json" handleAs="json"
           response="{{response}}"></core-ajax>
...

responseChanged: function() {
  //this.response is your JSON
}

http://www.polymer-project/docs/elements/core-elements.html#core-ajax

发布评论

评论列表(0)

  1. 暂无评论