为什么 Polmyer iron-ajax 在参数更改时不会自动请求?
Why Polmyer iron-ajax will not auto request when params change?
HTML
<iron-ajax auto id="ajaxWeapons"
verbose="true"
url="[[api.items.url]]"
params="[[api.items.params]]"
debounce-duration="100"
handle-as="json"
last-response="{{items}}"></iron-ajax>
脚本
app.view = 'category';
app.api.items.params.page = page;
app.api.items.params.category = this.categories.find((v)=>v.code === category).id;
铁-ajax只会请求一次,但是我换了params.category
就不会再请求了,怎么办?
编辑
如果我app.set('api.weapons.params',{size:18,category:1})
,熨斗-ajax会再次请求,app.set('api.weapons.params.category',1)
不会。
更新
我尝试使用计算绑定来解决这个问题
HTML
<iron-ajax auto id="ajaxWeapons"
verbose="true"
url="[[api.weapons.url]]"
params="{{getParams(api.weapons.params,api.weapons.params.size,api.weapons.params.category)}}"
debounce-duration="100"
handle-as="json"
last-response="{{weapons}}"></iron-ajax>
脚本
app.getParams = function (params) {
console.log('Do params ', params);
return this.extend({}, params);
};
但这不是预期的工作,getParams
永远不会被调用,甚至 api.weapons.params,api.weapons.params.size,api.weapons.params.category
都是 != undefined
。
params 对象未收到子项更改的通知,如您所知,set
或 notifyPath
无法解决问题。
目前我能让这个场景起作用的唯一方法是在参数更改后直接调用 generateRequest
。
this.$.ajaxWeapons.generateRequest();
要在需要时动态调用它,您可以添加观察者:
observers: {
'_testParamsChanged(testParams.*)'
},
_testParamsChanged: function(changeRecord) {
this.$.ajaxWeapons.generateRequest();
}
我认为你应该使用两种方式的数据绑定。 Polymer's documentation 表示:
Square brackets ([[]]) create one-way bindings. Data flow is downward, host-to-child, and the binding never modifies the host property.
Curly brackets ({{}}) create automatic bindings. Data flow is one-way
or two-way, depending whether the target property is configured for
two-way binding.
所以,我想你的代码应该是这样的:
<iron-ajax auto id="ajaxWeapons"
verbose="true"
url="{{api.items.url}}"
params="{{api.items.params}}"
debounce-duration="100"
handle-as="json"
last-response="{{items}}"></iron-ajax>
甚至,您可能需要使用观察者来通知对象属性的更改。 Here 你可以找到一个例子观察路径变化。
编辑
来自 <iron-ajax>
documentation:
With auto set to true, the element performs a request whenever its url, params or body properties are changed. Automatically generated requests will be debounced in the case that multiple attributes are changed sequentially.
Note: The params attribute must be double quoted JSON.
You can trigger a request explicitly by calling generateRequest on the
element.
这就是为什么 'app.set (' api.weapons.params.category ', 1) ` 不工作的原因。
HTML
<iron-ajax auto id="ajaxWeapons"
verbose="true"
url="[[api.items.url]]"
params="[[api.items.params]]"
debounce-duration="100"
handle-as="json"
last-response="{{items}}"></iron-ajax>
脚本
app.view = 'category';
app.api.items.params.page = page;
app.api.items.params.category = this.categories.find((v)=>v.code === category).id;
铁-ajax只会请求一次,但是我换了params.category
就不会再请求了,怎么办?
编辑
如果我app.set('api.weapons.params',{size:18,category:1})
,熨斗-ajax会再次请求,app.set('api.weapons.params.category',1)
不会。
更新
我尝试使用计算绑定来解决这个问题
HTML
<iron-ajax auto id="ajaxWeapons"
verbose="true"
url="[[api.weapons.url]]"
params="{{getParams(api.weapons.params,api.weapons.params.size,api.weapons.params.category)}}"
debounce-duration="100"
handle-as="json"
last-response="{{weapons}}"></iron-ajax>
脚本
app.getParams = function (params) {
console.log('Do params ', params);
return this.extend({}, params);
};
但这不是预期的工作,getParams
永远不会被调用,甚至 api.weapons.params,api.weapons.params.size,api.weapons.params.category
都是 != undefined
。
params 对象未收到子项更改的通知,如您所知,set
或 notifyPath
无法解决问题。
目前我能让这个场景起作用的唯一方法是在参数更改后直接调用 generateRequest
。
this.$.ajaxWeapons.generateRequest();
要在需要时动态调用它,您可以添加观察者:
observers: {
'_testParamsChanged(testParams.*)'
},
_testParamsChanged: function(changeRecord) {
this.$.ajaxWeapons.generateRequest();
}
我认为你应该使用两种方式的数据绑定。 Polymer's documentation 表示:
Square brackets ([[]]) create one-way bindings. Data flow is downward, host-to-child, and the binding never modifies the host property.
Curly brackets ({{}}) create automatic bindings. Data flow is one-way or two-way, depending whether the target property is configured for two-way binding.
所以,我想你的代码应该是这样的:
<iron-ajax auto id="ajaxWeapons"
verbose="true"
url="{{api.items.url}}"
params="{{api.items.params}}"
debounce-duration="100"
handle-as="json"
last-response="{{items}}"></iron-ajax>
甚至,您可能需要使用观察者来通知对象属性的更改。 Here 你可以找到一个例子观察路径变化。
编辑
来自 <iron-ajax>
documentation:
With auto set to true, the element performs a request whenever its url, params or body properties are changed. Automatically generated requests will be debounced in the case that multiple attributes are changed sequentially.
Note: The params attribute must be double quoted JSON.
You can trigger a request explicitly by calling generateRequest on the element.
这就是为什么 'app.set (' api.weapons.params.category ', 1) ` 不工作的原因。