I've been trying to get json data from the GW2 server, which didn't work with iron-ajax due to cross-domain issues. Than I tried this byutv-jsonp and that worked.
The problem now is that I get a response with the right data, but it's not triggering the on-response handler. So the data is never put inside the variable :/
Anyone has a tip, hint or clue?!
Source of byutv
http://coderfin.github.io/byutv-jsonp/components/byutv-jsonp/
<dom-module id="about-pagina">
<template>
<style include="shared-styles">
:host {
display: block;
}
</style>
<h1>about-pagina</h1>
<vaadin-grid id="grid" items="[[_c_studenten]]" selected-items="{{selected}}">
<vaadin-grid-column>
<template class="header">Account Name</template>
<template>
<div class="capitalized">[[item.name]]</div>
</template>
</vaadin-grid-column>
<vaadin-grid-column>
<template class="header">Rank</template>
<template>
<div class="capitalized">[[item.rank]]</div>
</template>
</vaadin-grid-column>
<vaadin-grid-column>
<template class="header">Date Joined</template>
<template>
<div class="capitalized">[[item.joined]]</div>
</template>
</vaadin-grid-column>
<vaadin-grid-column>
<template class="header">Docent</template>
<template>
<div class="capitalized">[[item.docent]]</div>
</template>
</vaadin-grid-column>
<vaadin-grid-column>
<template class="header">Lokaal</template>
<template>
<div class="capitalized">[[item.locatie]]</div>
</template>
</vaadin-grid-column>
<vaadin-grid-column>
<template class="header">Klas</template>
<template>
<div class="capitalized">[[item.klas]]</div>
</template>
</vaadin-grid-column>
</vaadin-grid-column>
</vaadin-grid>
<paper-button on-tap="derp" raised>Annuleren</paper-button>
<byutv-jsonp
id="ajax_members_get"
method="GET"
url="https://api.guildwars2.com/v2/guild/secretcode/members"
params='{"access_token":"secretcode"}'
handle-as="json"
last-response="{{_c_studenten}}"
on-response="_members_get_response_handler">
</byutv-jsonp>
</template>
<script>
Polymer({
is: 'about-pagina',
properties: {
c_visible: {
type: Boolean, /* true when element is the active visible item */
value: false,
observer: '_initializing',
},
_c_studenten: {
type: Array,
},
},
_initializing : function() {
if (this.c_visible) {
console.log("about-pagina");
this._members_get_request_handler();
}
},
_members_get_request_handler: function() {
console.log("_members_get_request_handler");
this.$.ajax_members_get.contentType="application/json";
this.$.ajax_members_get.body={
};
this.$.ajax_members_get.generateRequest();
},
_members_get_response_handler: function(request) {
console.log("_members_get_response_handler aantal studenten="+request.detail.response.length);
this._c_studenten = request.detail.response;
},
derp: function(){
console.log([[_c_studenten]]);
}
});
</script>
</dom-module>
As you are specifying _c_studenten's value in _members_get_response_handler function, you can remove last-response="{{_c_studenten}}" from the iron-ajax. That prevents setting the value twice. Now you can make sure that your data is in Array and not as Object (vaadin-grid requires array data). That probably does the trick. If not, make sure the format of data that iron-ajax fetches.
Related
How do I pass a slot or a prop to a layout component in inertia?
For example, heres a ForgotPassword component:
<template>
<slot name="title">Forgot Password</slot>
Forgot pw stuff goes here...
</template>
<script>
import CardLayout from "#/Layouts/CardLayout";
export default {
layout: CardLayout,
}
Here is the CardLayout component:
<template>
<h1>{{ $slots.title }}</h1>
<slot/>
</template>
Nothing shows up inside the h1 tag...
Adding this here as the solution above does not work. You can pass data to the Layout component by setting a prop value
layout: (h, page) => h(Layout, { somePropDataDefinedInLayout: value }, () => page)
// CardLayout
<template>
<h1><slot name="title" /></h1>
<slot />
</template>
// ForgotPassword
<template>
<template #title>Forgot Password</template>
Forgot pw stuff goes here...
</template>
I'm trying to pass an event to the parent component from the child component. I'm new to this and stuck at it. Basically, it is a dashboard having just a side panel and displaying area. The displaying area is in the dashboard template itself without a separate component.
This is my code for the dashboard.vue
<template>
<app-layout title="Dashboard">
<template #header>
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
Dashboard
</h2>
</template>
<div class="h-screen bg-white overflow-hidden shadow-xl grid grid-cols-5">
<click/>
<div>
{{msg}}
</div>
</div>
</app-layout>
</template>
<script>
import AppLayout from '#/Layouts/AppLayout.vue'
import click from '#/Components/click.vue'
export default {
components: {
AppLayout,
click,
},
data(){
return{
msg:'1'
}
},methods:{
addValue:function(){
this.msg++
}
}
}
</script>
And This is my click.vue component
<template>
<div>
<button v-on:click="add" class="border-solid font-semibold hover:bg-blue-600 hover:text-white border-b-2 w-full py-5" id="click">Click ME 1</button>
</div>
</template>
<script>
export default {
methods:{
add:function(){
this.$parent.$emit('addValue');
}
}}
</script>
I'm trying to trigger the addValue() in Dashboard.vue from Click.vue
You could use inject/provide pattern :
in root component :
methods:{
addValue:function(){
this.msg++
}
},
provide: function () {
return {
addValue: this.addValue
}
}
in grandchild component inject the method and run it from your method:
export default {
inject: ['addValue'],
methods:{
add:function(){
this.addValue();
}
}}
I have a Vue component using with Laravel app:
resources/assets/js/app.js:
Vue.component('auth-form', require('./components/AuthForm.vue'));
const app = new Vue({
el: '#app',
data: {
showModal: false
}
});
AuthForm.vue:
<template>
<div v-if="showModal">
<transition name="modal">
<div class="modal-mask">
<div class="modal-wrapper">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" #click="showModal=false">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
modal body
</div>
</div>
</div>
</div>
</div>
</transition>
</div>
</template>
<script>
export default {
name: "auth-form"
}
</script>
<style scoped>
...
</style>
I'm using component inside blade template:
<div id="app">
...
<button id="show-modal" #click="showModal = true">Auth</button>
...
<auth-form></auth-form>
</div>
And I'm getting error
Property or method "showModal" is not defined on the instance but referenced during render.
What's wrong with my component?
I used this JSFiddle as example.
The reason is you have defined showModel in the root component and AuthForm is a child of this.
change the script in AuthForm.vue to:
<script>
export default {
name: "auth-form",
data:function(){
return {
showModal: false
}
}
}
</script>
Or you could write a computed method to get the value from the parent component.
edit:
ahh ok i see what you require. you will need to use properties instead
blade template
<div id="app">
<button id="show-modal" #click="showModal = true">Auth</button>
<auth-form :show.sync="showModal"></auth-form>
</div>
script in AuthForm.vue
<script>
export default {
name: "auth-form",
props:['show'],
computed:{
showModal:{
get:function(){
return this.show;
},
set:function(newValue){
this.show = newValue;
}
}
}
}
</script>
showModal is a data item in the parent Vue, and not in the component. Since you want them to be the same thing, you should pass showModal to the child component as a prop. The click in the child component should emit an event that the parent handles (by changing the value).
Is there a way to select an element in a nested template? I have to access ajax within the dom-bind template so I can change the params or url.
<template>
<div class="listing" id="list">
<template is="dom-bind" id="tmp">
<iron-ajax id="bin" url="http://api.com/" last-response="{{data}}" handle-as="json" auto></iron-ajax>
<iron-list items="[[data]]" as="item">
<template>
<div class="list">
<div class="pic">
<iron-icon icon="[[item.type]]" class="icon"></iron-icon>
<iron-icon class="action"></iron-icon>
</div>
<div class="cont horizontal layout">
<span class="name">[[item.name]]</span>
<span class="date">[[item.date]]</span>
<span class="time">[[item.time]]</span>
<span class="note">[[item.note]]</span>
</div>
<paper-ripple></paper-ripple>
</div>
</template>
</iron-list>
</template>
</div>
</template>
Here I have a property named filter. When filter changes it automatically fires _filterChanged. I am able to access the #tmp template but I am unable to access the iron-ajax within the #tmp template.
Polymer({
is: "event-log",
properties: {
filter: {
type: String,
value: "all",
observer: '_filterChanged'
}
},
_filterChanged: function() {
var ajax = this.$.tmp.getElementById("bin");
console.log(ajax); // Returns null
}
});
So I'm trying to dynamically generate a paper-dropdown-menu populated from an AJAX data source, which is working great using the code below:
<polymer-element name="my-element" attributes="selected">
<template>
<core-ajax auto url="/api/items/" response="{{items}}" handleAs="json"></core-ajax>
<paper-dropdown-menu selected="{{selected}}">
<template repeat="{{items}}">
<paper-item name="{{id}}" label="{{name}}"></paper-item>
</template>
</paper-dropdown-menu>
</template>
But if I set the initial selected item to be either the value of the published attribute, or a value that I set in the 'ready' callback, the dropdown menu item does not get selected when the items are loaded:
<script>
Polymer({
publish: {
selected: null
}
});
</script>
</polymer-element>
I understand that this is happening because the 'selected' property is being set before the template in the dropdown gets bound, but my question is whether there is a way to either 1) defer the 'selected' property change until after the template is bound or 2) otherwise reliably set an initially selected value for the dropdown menu?
One option would be to not render the dropdown until the data is available.
ex: http://jsbin.com/piyogo/13/edit
<polymer-element name="foo-drop">
<template>
<core-ajax auto
url="http://www.json-generator.com/api/json/get/bJMeMASvTm?indent=2"
response="{{items}}"
handleas="json">
</core-ajax>
<template if="{{items}}">
<paper-dropdown-menu selected="{{selected}}">
<template repeat="{{item in items}}">
<paper-item label="{{item.name}}"></paper-item>
</template>
</paper-dropdown-menu>
</template>
</template>
<script>
Polymer({
publish: {
selected: null
}
});
</script>
</polymer-element>
<foo-drop selected="2"></foo-drop>
For Polymer 1.0 (and later), the same can be achieved with the following code:
<iron-ajax auto url={{url}} handle-as="json" last-response="{{items}}"></iron-ajax>
<template is="dom-if" if="{{items}}">
<paper-dropdown-menu-light label="[[label]]" no-animations selected-item="{{selected}}">
<paper-listbox class="dropdown-content">
<template is="dom-repeat" items="[[items]]">
<paper-item>[[item.name]]</paper-item>
</template>
</paper-listbox>
</paper-dropdown-menu-light>
</template>