Chrome extension converting backbone.js collections to regular JavaScript array
-
12-12-2019 - |
Question
In my background script:
var collection = Backbone.Collection.extend({});
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
sendResponse(new collection());
}
In my browser_action's javascript:
chrome.tabs.getSelected(null, function(tab) {
chrome.extension.sendRequest({
action: "someAction",
tab: tab
},
function(collection) {
// collection is now a JS array, rather than Backbone.Collection
});
});
As mentioned in the comment above the 'collection' argument in the sendRequest callback turns out to be a regular JS array, rather than Backbone.Collection.
Is this a sanitisation artefact / security measure taken by chromium? Is there any way to pass a Backbone.Collection via sendRequest?
Solution
From the onRequest
documentation it says that the argument to sendResponse
should be a JSON-ifiable object, so I'm assuming that the Collection's toJSON
method is being called, leaving you with just the data.
If you have the Collection definition in the target script you could instantiate a new object with the same data.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow