Last active
August 29, 2015 14:22
-
-
Save Scarygami/c0c40b939c863e91cf8b to your computer and use it in GitHub Desktop.
Data binding vs. event handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dom-module id="request-data-binding1"> | |
<template> | |
<discovery-api-elements | |
name="plus" version="v1"></discovery-api-elements> | |
<google-signin | |
client-id="..." | |
scopes="..." | |
is-authorized="{{isAuthorized}}"></google-signin> | |
<plus-activities-list | |
user-id="me" | |
collection="public" | |
on-success="_handleResponse" | |
auto$="[[isAuthorized]]"></plus-activities-list> | |
<template is="dom-repeat" items="[[activities]]"> | |
<span>[[item.published]]</span> <a href$="[[item.url]]">[[item.title]]</a><br> | |
</template> | |
</template> | |
<script> | |
(function () { | |
Polymer({ | |
is: "request-data-binding1", | |
properties: { | |
isAuthorized: Boolean, | |
activities: Array | |
}, | |
_handleResponse: function (e) { | |
this.activities = e.detail.items; | |
} | |
}); | |
}()); | |
</script> | |
</dom-module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dom-module id="request-data-binding2"> | |
<template> | |
<discovery-api-elements | |
name="plus" version="v1"></discovery-api-elements> | |
<google-signin | |
client-id="..." | |
scopes="..." | |
is-authorized="{{isAuthorized}}"></google-signin> | |
<plus-activities-list | |
user-id="me" | |
collection="public" | |
response="{{response}}" | |
auto$="[[isAuthorized]]"></plus-activities-list> | |
<template is="dom-repeat" items="[[activities]]"> | |
<span>[[item.published]]</span> <a href$="[[item.url]]">[[item.title]]</a><br> | |
</template> | |
</template> | |
<script> | |
(function () { | |
Polymer({ | |
is: "request-data-binding2", | |
properties: { | |
isAuthorized: Boolean, | |
response: Object, | |
activities: { | |
type: Array, | |
computed: '_parseActivities(response, isAuthorized)' | |
} | |
}, | |
_parseActivities: function (response, isAuthorized) { | |
if (!isAuthorized || !response) { return []; } | |
// you could do any preprocessing of the data here | |
return response.items; | |
} | |
}); | |
}()); | |
</script> | |
</dom-module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dom-module id="request-event-handling"> | |
<template> | |
<discovery-api-elements | |
name="plus" version="v1"></discovery-api-elements> | |
<google-signin | |
client-id="..." | |
scopes="..." | |
is-authorized="{{isAuthorized}}"></google-signin> | |
<plus-activities-list | |
id="listRequest" | |
user-id="me" | |
collection="public" | |
on-success="_handleResponse"></plus-activities-list> | |
<template is="dom-repeat" items="[[activities]]"> | |
<span>[[item.published]]</span> <a href$="[[item.url]]">[[item.title]]</a><br> | |
</template> | |
</template> | |
<script> | |
(function () { | |
Polymer({ | |
is: "request-event-handling", | |
properties: { | |
isAuthorized: { | |
type: Boolean, | |
observer: "_authorizedChanged" | |
}, | |
activities: Array | |
}, | |
_authorizedChanged: function (isAuthorized) { | |
if (!isAuthorized) { | |
this.activities = []; | |
return; | |
} | |
// Trigger the API request | |
this.$.listRequest.go(); | |
}, | |
_handleResponse: function (e) { | |
this.activities = e.detail.items; | |
} | |
}); | |
}()); | |
</script> | |
</dom-module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dom-module id="signin-data-binding"> | |
<template> | |
<google-signin | |
client-id="..." | |
scopes="..." | |
is-authorized="{{isAuthorized}}"></google-signin> | |
<div hidden$="[[!isAuthorized]]">Signed in as <span>{{user.name}}</span></div> | |
</template> | |
<script> | |
Polymer({ | |
is: 'signin-data-binding', | |
properties: { | |
user: { | |
type: Object, | |
computed: '_computeUser(isAuthorized)' | |
}, | |
isAuthorized: Boolean | |
}, | |
_computeUser: function (isAuthorized) { | |
if (!isAuthorized) { return undefined; } | |
var profile = gapi.auth2.getAuthInstance().currentUser.get().getBasicProfile(); | |
return { | |
id: profile.getId(), | |
name: profile.getName(), | |
image: profile.getImageUrl() | |
}; | |
} | |
}); | |
</script> | |
</dom-module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dom-module id="signin-event-handling"> | |
<template> | |
<google-signin | |
client-id="..." | |
scopes="..." | |
on-google-signin-success="_handleSignin" | |
on-google-signed-out="_handleSignout"></google-signin> | |
<div hidden$="[[!isAuthorized]]">Signed in as <span>{{user.name}}</span></div> | |
</template> | |
<script> | |
Polymer({ | |
is: 'signin-event-handling' | |
properties: { | |
user: Object, | |
isAuthorized: Boolean | |
}, | |
_handleSignin: function () { | |
var profile = gapi.auth2.getAuthInstance().currentUser.get().getBasicProfile(); | |
this.isAuthorized = true; | |
this.user = { | |
id: profile.getId(), | |
name: profile.getName(), | |
image: profile.getImageUrl() | |
}; | |
}, | |
_handleSignout: function () { | |
this.user = undefined; | |
this.isAuthorized = false; | |
} | |
}); | |
</script> | |
</dom-module> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment