2015-04-03 48 views
2

在Facebook的doc它說是否反應原生模塊發送事件到javascript文檔是否正確?

- (void)calendarEventReminderReceived:(NSNotification *)notification 
{ 
    NSString *eventName = notification.userInfo[@"name"]; 
    [self.bridge.eventDispatcher sendAppEventWithName:@"EventReminder" 
               body:@{@"name": eventName}]; 
} 

JavaScript代碼可以訂閱這些事件:

var subscription = DeviceEventEmitter.addListener(
    'EventReminder', 
    (reminder) => console.log(reminder.name) 
); 

但我永遠不能得到這個工作。這應該是sendDeviceEventWithName嗎?也應該本地模塊總是單身?

回答

2

目前,沒有。請看到這個問題:

https://github.com/facebook/react-native/issues/394

更改例如Objective-C的調用sendDeviceEventWithName 而不是sendAppEventWithName解決了這個問題。

這似乎證實了你的建議。有一個拉請求等待被合併爲這樣:

https://github.com/facebook/react-native/pull/530

+0

酷,謝謝。在我更改爲sendDeviceEventWithName後,它似乎工作正常。 – Matthew 2015-04-04 17:35:47

+0

我實際上無法得到這個工作我自己!有什麼機會可以將您的(簡化)代碼發佈到某個地方嗎? – 2015-04-04 18:31:16

+0

訣竅是讓你的natvie模塊成爲一個單身人士。只需在您的實例中檢查_bridge並確保它不是零。我不確定這是一個功能還是錯誤。 – Matthew 2015-04-06 05:52:32