1

我目前正在开发一个具有brand_field_id参数之一的应用程序。它将保存“品牌”的自定义票证字段 ID。问题是:它不是必需的,因为用户可能想要或不使用它提供的功能。预期行为:

  • 如果此字段填充了 ID,.change则将在自定义字段上添加它的事件侦听器(并且事情将完成);
  • 如果将其留空,则不会发生任何事情。这意味着用户选择不使用它。

Zendesk 已经为自定义工单字段更改事件提供了完美的监听器,它的工作方式如下(动态使用brand_field_id):

events: {
  "ticket.custom_field_ticket.custom_field_{{brand_field_id}}.changed": "myCustomFieldChangedFunction"
}

... aaand 如果应用程序在为空时没有崩溃,brand_field_id那将是非常棒的。

由于我不希望应用程序在brand_field_id留空时崩溃,因此我想在添加事件侦听器之前对其进行验证,但没有设法完成。我在app.created事件中尝试了以下代码的一些变体,但都没有成功。

if(!_.isEmpty(this.setting('brand_field_id'))){
  console.log('Brand change event added');
  this.events['ticket.custom_field_ticket.custom_field_{{brand_field_id}}.changed'] = 'brandChangedHandler';
}

console.log被解雇,所以验证是好的。不幸的是,该事件永远不会为自定义字段触发。

所以我的问题是:如何在旅途中添加事件侦听器?还是有另一种方法来实现我所需要的?


我什至在 Zendesk 社区的一个类似线程上发布了这个问题,但到目前为止还没有答案。我在那里找到的解决方法确实有效,但我不是很喜欢它:

events: {
  // [...]
  '*.changed': 'anyFieldChangedHandler'
},
// [...]
anyFieldChangedHandler: function(e){
  // If the changed field was {brand_field_id}
  if(!_.isEmpty(this.setting('brand_field_id')) && e.propertyName === 'ticket.custom_field_'+ this.setting('brand_field_id')){
    this.brandChangedHandler(e);
  }
},

它太宽泛了,每当票证上的任何字段发生变化时都会触发。我想找到一个更好、更干净、更优雅的解决方案。

4

1 回答 1

0

我知道要找到与 Zendesk Apps 框架相关的答案是多么困难,所以我在同一条船上。

我会做这样的事情

events: {
  "ticket.custom_field_ticket.custom_field_{{brand_field_id}}.changed":"itChanged"
},
itChanged: function(){
      if(ticket.custom_field_ticket.custom_field_{{brand_field_id}} !== null && ticket.custom_field_ticket.custom_field_{{brand_field_id}} !== undefined){
          //Your custom field exists and has a value. Do something with it.
      }
      else{
          //Your custom field does not exist and/or does not have a value. do nothing
      }
}

像这样的东西适用于您的应用程序吗?我不确定这是否具有相同的功能,!_.isEmpty()但您可以尝试一下。我在我的应用程序中经常使用它,有时只是作为一种安全措施,以防我的应用程序加载速度比实际票证快,这会导致应用程序加载时某些值“未定义”。

希望这可以帮助

于 2015-06-24T03:08:44.487 回答