【发布时间】:2017-02-15 01:56:36
【问题描述】:
目前我的模板中有这个:
-
方式 1
<template is="dom-if" if="{{_showCancel(userData.generic.id,info.userId,info._children.gigId.userId,info.accepted,info.cancelled,info.paymentTerms)}}">
然后在我的元素中:
_showCancel: function(viewerUserId,offerUserId,gigUserId, accepted, cancel, paymentTerms) {
// Offer needs to be accepted and NOT cancelled
if (!info.accepted || info.cancelled) return false;
// Gig owner can ALWAYS cancel an offer
if (viewerUserId == gigUserId ) return true;
// Offer maker can only cancel if the gig involved pre-payment
if (viewerUserId == offerUserId && paymentTerms != 'on-day') return true;
return false;
},
这个函数的参数太多了吗?
- 方式 2
我应该只用这样的东西吗:
<template is="dom-if" if="{{_showCancel(userData, info)}}">
- 方式 3
虽然我想检查它们的子属性是否也发生变化......所以我需要:
<template is="dom-if" if="{{_showCancel(userData, info, userData.*, info.*)}}">
- 方式 4
但话又说回来,我可能应该只查找属性并像这样使用value 属性:
<template is="dom-if" if="{{_showCancel(userData.*, info.*)}}">
然后函数将是:
_showCancel: function(userDataObs, infoObs) {
var userData = userDataObs.value;
var info = infoObs.value;
if( !userData || !info) return;
...
问题:
- 您是否发现方法 1 到 4 存在任何根本性错误?
- 方式 1 真的是最好的方法吗? (现在感觉就是这样)
- 方式 3 是可接受的模式吗?
附录
遇到这种情况我会怎么做:
<paper-button
raised
hidden$="{{!_showCancel(item.id,userData.config.usersCategories,userData.config.userCategories,userData.config.usersCategories.*)}}"
>
Cancel
</paper-button>
(注意:usersCategories 是 Array)
而_showCancel 是:
_showCancel: function(categoryId, userCategories) {
for (var k in usersCategories) {
var $ucat = usersCategories[k];
if ($ucat.categoryId == categoryId) {
if ($ucat.status == 'applied' || $ucat.status == 'granted') return true;
}
}
return false;
},
重点是:我希望既能轻松访问usersCategories,又不想从“奇怪”的数组修饰符等中获取值。那么,这是一个好的模式吗?
【问题讨论】:
-
补充:如果你想检测数组突变,你需要听
<array>.*或<array>.splices(see spec)。如果你不喜欢 splices(strange 修饰符),那么你可以在你的函数中忽略它们。但是您仍然应该同时传递数组和.*或.splices变量。没有它,更改将无法正确传播。您当前的代码实际上是可以的。除了for/initeration。这不是迭代数组的好方法,使用简单的for和i作为索引。 -
我最近发现我可以用 for 语句迭代一个数组。我发现它更容易!这有什么问题?
-
一切都好,我刚刚想通了 :D stackoverflow.com/questions/500504/…
标签: data-binding polymer web-component