2017-10-20 126 views
1

我想注入channelData與每個消息是從一個殭屍網頁控制在一個頁面發送。我環顧四周,發現這個示例(https://cmsdk.com/javascript/how-to-send-custom-channel-data-when-using-web-chat-client-with-bot-framework.html),我的代碼看起來像下面的代碼。發送channelData到網絡聊天與每條消息

問題是,這可以在Chrome中運行,但擴展運算符(...)在Edge或IE上不起作用。是否有可用於所有瀏覽器的替代語法?

var user = { 
    id: '@User.Identity.Name', 
    name: '@User.Identity.Name' 
}; 

var bot = { 
    id: BotId, 
    name: 'BotName' 
}; 

var botConnect = new BotChat.DirectLine({ 
    secret: '@ViewData["BotSecret"]', 
    webSockets: 'true' 
}); 

var v = { ...botConnect }; 
debugger; 

BotChat.App({ 
    botConnection: { 
     ...botConnect, 
     postActivity: activity => { 
      activity.channelData = { 
       StudentId: '@User.Identity.Name' 
      }; 
      return botConnect.postActivity(activity); 
     } 
    }, 
    user: user, 
    bot: bot, 
    resize: 'detect' 
}, document.getElementById("bot")); 
+0

邊緣應該支持每http://kangax.github.io/compat蔓延-table/es6 /#test-spread _(...)_運營商 –

+0

我認爲您需要手動啓用實驗性功能才能正常工作,並且不希望最終用戶必須這樣做。我不認爲有一個解決方法爲IE瀏覽器。 – GaboG

回答

0

它看起來像巴貝爾具有a plugin使用Object.assign,其將傳播到操作者的等效代碼。這並不能完全解決你的問題,因爲IE仍然不支持Object.assign - 在Babel的情況下,Object.Assign包含一個polyfill。雖然在您的項目中包含Babel可能會過度殺傷,但MDN has sample code可能包含更合理的簡單獨立Object.assign填充。

如果這是一個愉快的依賴,那麼一旦Object.assign是提供給您的跨瀏覽器,巴貝爾文檔建議的兩行代碼是等價的:

在:

z = { x, ...y }; 

出:

z = Object.assign({ x }, y); 
0

只是CLO在這個循環中唱歌,我和一些知道他們的JS的人一起工作,我們實現了一個可以在IE,Chrome和Edge上工作的「傳播等效」功能(還沒有在Safari中測試過,但我猜它也應該在那裏工作)。

IE不喜歡=>運營商,所以我們改變了一個功能太,這裏是生成的代碼:

var user = { 
    id: '@User.Identity.Name', 
    name: '@User.Identity.Name' 
}; 

var bot = { 
    id: 'TheBotId', 
    name: 'TheBotName' 
}; 

var botConnect = new BotChat.DirectLine({ 
    secret: 'TheBotSecret', 
    webSockets: 'true' 
}); 

// Spread equivalent function 
function getBotConnectionDetail(botconnection) { 
    var botConnectionDetail = {}; 
    var keys = Object.keys(botconnection); 
    for (var i = 0; i < keys.length; i++) { 
     botConnectionDetail[keys[i]] = botconnection[keys[i]]; 
    }; 
    botConnectionDetail['postActivity'] = function (activity) { 
     activity.channelData = { 
      StudentId: '@User.Identity.Name' 
     }; 
     return botconnection.postActivity(activity) 
    }; 
    return botConnectionDetail; 
} 

// Invokes Bot 
BotChat.App({ 
     botConnection: getBotConnectionDetail(botConnect), 
     user: user, 
     bot: bot, 
     resize: 'detect' 
    }, 
    document.getElementById("bot") 
);