Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

glade ad element with a single gladeReady listener #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions glade-ad-single-listener.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<!--
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="/bower_components/polymer/polymer.html">
<dom-module id="glade-ad">
<template>
<style>
:host {
display: block;
width: 100%;
height: 100%;
}
</style>
<div class="ad_slot_placeholder"></div>
</div>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra </div>

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops; fixed.

</template>
<script>
(function() {
var ad_slot;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is used anywhere.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left over from playing :) Fixed.

var gladeReady = !!(window.glade && window.glade.run); // Maybe Glade has already loaded?
var elementsWaitingForGlade = [];
if (!gladeReady) {
window.addEventListener('gladeReady', function listener() {
window.removeEventListener('gladeReady', listener);
gladeReady = true;
for (var i = 0; i < elementsWaitingForGlade.length; i++) {
elementsWaitingForGlade[i]._load();
}
});
}

Polymer({
is: 'glade-ad',
properties: {
adSlotId: {
type: String,
},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Properties that only specify type have a shorthand form you can use if you want:
adSlotId: {type: String}, -> adSlotId: String,

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, much nicer!

dataAdUnitPath: {
type: String
},
width: {
type: String
},
height: {
type: String
},
dataClickUrl: {
type: String
},
dataPageUrl: {
type: String
},
dataJson: {
type: String
}
},

observers: [
'propertiesChanged(adSlotId, dataAdUnitPath, width, height)'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

propertiesChanged no longer exists, so I think you can remove this whole observers list.

(tangent) If I remember correctly, all of the attributes will have been set to their respective properties by the time ready is called. Given that a <glade-ad> always waits until ready before it attempts to run _load (either running sync or inserting itself into the queue to be called later), all the properties should be available in time.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it; removing the observers list.

],

ready: function() {
if (gladeReady) {
this._load();
} else {
elementsWaitingForGlade.push(this);
}
},

_load: function() {
Polymer.RenderStatus.afterNextRender(this, function() {

var ad_slot = Polymer.dom(this.root).querySelector(".ad_slot_placeholder");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you change <div class="ad_slot_placeholder"></div> above to <div id="ad_slot_placeholder"></div> (since you only have one), you can access it here as this.$.ad_slot_placeholder. Polymer will find elements with ids in your template and add them as properties of this.$ automatically for you so you don't need to querySelector manually.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicer too :)

ad_slot.id = this.adSlotId;
ad_slot.setAttribute("data-ad-unit-path", this.dataAdUnitPath);
ad_slot.setAttribute("width", this.width);
ad_slot.setAttribute("height", this.height);

if (this.dataClickUrl) {
ad_slot.setAttribute("data-click-url", this.dataClickUrl);
}

if (this.dataPageUrl) {
ad_slot.setAttribute("data-page-url", this.dataPageUrl);
}

if (this.dataJson) {
ad_slot.setAttribute("data-json", this.dataJson);
}

glade.run(ad_slot);
});
},
});
})();
</script>
</dom-module>