How to render content with multiple AJAX calls with Polymer?

Denis Hoctor

I have a URLs array and after looping through it I'm unable to render my content. The normal binding doesn't seem to work. If I remove the repeat and work with 1 ajax endpoint things work, however I have a need to pull data from two endpoints and merge it for rendering. I also tried:

document.querySelector('polymer-letters').model = this.letters;

without any luck. What have I missed in Polymers pattern?

Working: https://gist.github.com/denishoctor/d857f11fa0be79c29fab

Broken: https://gist.github.com/denishoctor/6c36a9d9d310c81a8207

        <polymer-element name="polymer-letters" attributes="urls">
        <template>
            <h1>{{urls}}</h1>
            <template repeat="{{url in urls}}">
                <core-ajax url="{{url}}" auto response="{{resp}}" on-core-response="{{parse}}"></core-ajax>
            </template>
            <template repeat="{{ letter in letters }}">
              {{ letter }}
            </template>
        </template>
        <script>
            Polymer('polymer-letters', {
                ready: function() {
                    this.lettersOld = ['a', 'b', 'c'];
                },
                parse: function(event, detail, ajaxNode) {
console.log(event.detail.response)
                    this.letters = event.detail.response;
                },
                parse2: function(event, detail, ajaxNode) {
console.log(event.detail.response)
                    this.letters = event.detail.response;
                }
            });
        </script>
    </polymer-element>

    <script>
        var urls = ["data/letters.json"],
            polymerLetters = document.createElement("polymer-letters");

        polymerLetters.setAttribute('urls', JSON.stringify(urls));
        document.body.appendChild(polymerLetters);

        window.addEventListener('polymer-ready', function() {
console.dir(polymerLetters);
        });
    </script>
Jeff Posnick

I'm a little confused about your broken example, since you have things defined (e.g. parse2 and lettersOld) that are never used, and I'm not exactly sure what your AJAX call is supposed to return.

That being said, if I understand your general question, you're trying to specify multiple URLs which return some JSON content, and you want to display all of the content returned by all of the URLs in a <template> within your element.

If that's the case, here's an example of one way you could do that. (Live version at http://jsbin.com/qecat/5/edit)

<body>
  <polymer-element name="polymer-letters" attributes="urls">
    <link rel="import" href="https://cdn.rawgit.com/Polymer/core-ajax/master/core-ajax.html">
    <template>
      <h1>{{urls}}</h1>
      <template repeat="{{url in urls}}">
        <core-ajax url="{{url}}" auto handleAs="json" on-core-response="{{parse}}"></core-ajax>
      </template>
      <ul>
        <template repeat="{{ video in videos }}">
          <li>{{ video.title }}</li>
        </template>
      </ul>
    </template>
    <script>
      Polymer('polymer-letters', {
        videos: [],
        urls: [],
        parse: function(e) {
          this.videos = this.videos.concat(e.detail.response.data.items);
        }
      });
    </script>
  </polymer-element>

  <script>
    window.addEventListener('polymer-ready', function() {
      var urls = [
        "http://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&max-results=5&q=testing",
        "http://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&max-results=5&q=polymer"
      ];
      var polymerLetters = document.createElement("polymer-letters");

      polymerLetters.urls = urls;
      document.body.appendChild(polymerLetters);
    });
  </script>
</body>

You should set polymerLetters.urls to the JavaScript Array containing your URLs, since it's a published property of the Polymer element. You don't need to do anything with setAttribute and JSON-ifying your array.

You can use handleAs="json" in the <core-ajax> to automatically parse the JSON response.

I'm using some YouTube API URLs in my example, since I don't know what data/letters.json should return. You'd want to change the logic in the parse handler to properly process what's returned from your actual data source.

Most importantly, I'm setting this.videos (analogous to your this.letters) to the concatenated version of all of the results that are returned each time parse is called. Otherwise, each time parse is called for each AJAX response, you'd clobber the values that were returned from previous responses.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to avoid multiple AJAX calls?

From Dev

How To Reduce Multiple Ajax Calls

From Dev

How to avoid multiple AJAX calls?

From Dev

AJAX multiple ajax calls

From Dev

How to call multiple ajax calls on a view?

From Dev

Polymer iron-ajax interval calls

From Dev

Ajax with polymer. render after response

From Dev

Restrictions of Multiple AJAX Calls?

From Dev

Multiple Ajax Calls

From Dev

Waiting for multiple Ajax calls

From Dev

Multiple AJAX calls optimisations

From Dev

Multiple API calls inside a Polymer Custom Element

From Dev

How to modify ajax calls or promises chain so that multiple variables (from an array) can be used in separate ajax calls?

From Dev

How can I have multiple ajax calls in one route?

From Dev

how to pass multiple ajax calls in a single django view

From Dev

How to set delay for multiple ajax calls and bind a stop button to them?

From Dev

How do multiple components target the same "f:ajax render "target?

From Dev

when multiple ajax calls done

From Dev

Multiple Ajax Calls, One Callback

From Dev

show() overwritten multiple ajax calls

From Dev

Multiple Ajax Calls, One Callback

From Dev

AngularJS : multiple asynchronous AJAX calls

From Dev

How to render shadow DOM in a polymer custom element

From Dev

How to extend multiple elements with Polymer

From Dev

Polymer 1.0: Multiple calls to send() method of iron-request

From Dev

Using rails and ajax to dynamically render content.

From Dev

Polymer: How to access elements within the 'content' tag

From Dev

Polymer: How to watch for change in <content> properties

From Dev

Google Polymer - how to add content to Neon elements

Related Related

HotTag

Archive