2016-07-07 32 views
1

以下是我迄今爲止所做的以及我正在嘗試做的事情。用drupal 8中的Nodejs集成模塊更新文本

我做了什麼至今

  • 模塊的NodeJS和Drupal 8完成。示例模塊工作正常。
  • 在drupal 8中創建了一個簡單的模塊,它由一個簡單的表格
    和 組成。在其提交函數中,調用nodejs模塊函數將我的消息排入通道。
  • 在nodejs入隊消息中定義的Javascript回調函數。

我一直在努力當提交的文本形式acheive

  • 。只是爲了更新在Drupal 8塊(更新塊含量的hello world。)

問題

  • 我與相關的NodeJS JavaScript的回調不會被調用。

以下是我的代碼。

提交功能代​​碼

public function submitForm(array &$form, FormStateInterface $form_state) { 
/*$message->broadcast = TRUE; 
* This would normally be replaced by code that actually does something 
* with the title. 
*/ 
$title = $form_state->getValue('title'); 
$message = (object) array(
    'channel' => 'example', 
    'broadcast' => TRUE, 
    'callback' => 'example', 
    'data' => array(
      'message' => 'Hello World' 
    ), 
); 
nodejs_enqueue_message($message); 
drupal_set_message(t('You specified a title of %title.', ['%title' => $title])); 
} 

JavaScript回調代碼

(function ($) { 
Drupal.Nodejs.callbacks.example = { 
    //grab the message and inject into the header 
    callback: function (message) { 
     console.log('simple example'); 
     if(message.channel == 'example') { 
      $('#nodejs-selector').html(message.data.body); 
     } 
    } 
}; 
})(jQuery); 

任何幫助,我將非常感激。如果有人需要,我很樂意提供這方面的更多信息。

回答

1

你應該修改$消息

$message = (object) array(
    'channel' => 'example', 
    'broadcast' => TRUE, 
    'callback' => 'example', 
    'data' => array(
     'body' => 'Hello World' 
), 
); 

要訪問郵件正文爲message.data.body

+0

哦解決:)感謝您指出的問題。 – FaNtAMode