gpt4 book ai didi

javascript - polymer 下拉数据与 AJAX 响应绑定(bind)

转载 作者:行者123 更新时间:2023-12-03 09:55:18 24 4
gpt4 key购买 nike

我对 JS 和 Polymer 很陌生。不明白为什么这不起作用,我已经根据那里的几个主题重写了一百万次。

这个想法是有一个非常简单的下拉列表,其选择数据绑定(bind)到 AJAX 请求的响应。

根据日志记录,AJAX 完美返回,但数据未绑定(bind)。

有人可以帮忙吗?

AJAX 响应:

var text = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';

脚本和导入(可能超出了应有的范围,我正在尝试组件)

<script src="../../static/bower/webcomponentsjs/webcomponents.js"></script>
<script src="../../static/npm/jquery/dist/jquery.js"></script>
<script src="../../static/npm/webanimations/web-animations.min.js"></script>

<link rel="import" href="../../static/bower/font-roboto/roboto.html">
<link rel="import" href="../../static/bower/core-header-panel/core-header-panel.html">
<link rel="import" href="../../static/bower/core-toolbar/core-toolbar.html">
<link rel="import" href="../../static/bower/paper-tabs/paper-tabs.html">
<link rel="import" href="../../static/bower/paper-dropdown-menu/paper-dropdown-menu.html">
<link rel="import" href="../../static/bower/core-ajax/core-ajax.html">
<link rel="import" href="../../static/bower/polymer/polymer.html">
<link rel="import" href="../../static/bower/core-list/core-list.html">
<link rel="import" href="../../static/bower/core-collapse/core-collapse.html" >
<link rel="import" href="../../static/bower/core-menu/core-menu.html">
<link rel="import" href="../../static/bower/paper-dropdown/paper-dropdown.html">
<link rel="import" href="../../static/bower/paper-item/paper-item.html">

HTML

<body>

<polymer-element name="custom-selector" attributes="host data">
<template>
<template if="{{data.length == 0}}">
<core-ajax auto
url="dosql"
on-core-response="{{updateData}}"
on-core-error="{{onError}}"
response="{{yourData}}"
</core-ajax>
</template>
<paper-dropdown-menu label="Dropdown with Data" on-core-select="{{selectData}}">
<paper-dropdown class="dropdown">
<core-menu class="menu">
<template repeat="{{data in yourData}}">
<paper-item name="{{data.firstName}}">{{data.firstName}}</paper-item>
</template>
</core-menu>
</paper-dropdown>
</paper-dropdown-menu>
</template>

<script>
Polymer('custom-selector', {
ready: function () { console.log('firing ready');
this.host = this.host || 'example.com';
this.data = this.data || [];
},
onError: function (e, resp) { console.log('onError');
console.log('error!: ' + resp.response);
},
updateData: function (e, resp) { console.log('firing updateData');
console.log('AJAX response:');
console.log(JSON.parse(resp.response));
this.data = JSON.parse(resp.response);

console.log('this.data is now:');
console.log(this.data);
}
});
</script>
</polymer-element>



<custom-selector></custom-selector>

</body>

最佳答案

<template repeat="{{data in yourData}}">

yourData 不是数组,而是对象。您想要实现的目标是:

<template repeat="{{data in yourData.employees}}">

我冒昧地稍微改进了你的元素。我希望您能从中吸取教训,如果您需要进一步的解释,请随时询问。

<polymer-element name="custom-selector" attributes="host data">
<template>
<core-ajax
id="employeesgetter"
handleAs="json"
url="dosql"
on-core-error="{{onError}}"
response="{{data}}">
</core-ajax>
<paper-dropdown-menu label="Dropdown with Data" on-core-select="{{selectData}}">
<paper-dropdown class="dropdown">
<core-menu class="menu">
<template repeat="{{employee in data.employees}}">
<paper-item name="{{employee.firstName}}">{{employee.firstName}}</paper-item>
</template>
</core-menu>
</paper-dropdown>
</paper-dropdown-menu>
</template>

<script>
Polymer('custom-selector', {
data: {
employees: []
},
ready: function () {
console.log('firing ready');
this.host = this.host || 'example.com';
this.$.employeesgetter.go();
},
onError: function (e, resp) {
console.log('onError');
console.log('error!: ' + resp.response);
}
});
</script>
</polymer-element>

关于javascript - polymer 下拉数据与 AJAX 响应绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30757183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com