Invoking cards

Cards enhance the invocation framework's capabilities by allowing apps to share the UI and the app logic with other apps. You send a request to invoke a card in the same way as an invocation request for an app, which means that everything that you learned about sending an invocation request, applies to cards as well. Before your app can invoke cards, you must add the invocation plugins to your app. To add the plugins, on the command line, navigate to your project folder and run the following commands:

webworks plugin add com.blackberry.invoke
webworks plugin add com.blackberry.invoke.card

Here's how you can use the invocation framework to import a card from another app:

blackberry.invoke.invoke({
target: "com.acme.myapp",
action: "bb.action.VIEW",
type: "image/png",
    uri : "file://path/to/image.png"
}, onInvokeSuccess, onInvokeError);

Before a new card is invoked and allowed to be stacked, the BlackBerry 10 OS verifies that the client app is not already parenting a card. Unlike an app, a card may continue to exist in multiple instances, but a parent app (or card) can parent only one card at a time. This is important because multiple instances of a card may be running at the same time.

Cards support a "fire and forget" import model. However, for apps that require notifications and additional feedback, cards also provide peek notifications and response data.

Listen for peek events

Cards offer a parent card or an app the ability to receive notifications when a user peeks at another card. This is important if you want your app to display content differently during a peek event.

Here is how you can enable your app to listen for peek events:

function onChildCardStartPeekHandler(peekType) {
    console.log("The card started peeking.");
    if (peekType == "root") {
        updateContent(true);
    }
}
function onChildCardEndPeekHandler() {
    console.log("I am no longer being peeked at.");
}


document.addEventListener("onChildCardStartPeek", onChildCardStartPeekHandler);
document.addEventListener("onChildCardEndPeek", onChildCardEndPeekHandler);

Listen for a card response message

A card can send a response message to the parent app when a card completes its task. For example, a picker card can return the data that a user selects to the client app.

Here's how you can listen for card response messages:

function onCardClosedHandler(info) {
    console.log("Card was closed: " + info);
}
document.addEventListener('onChildCardClosed',onCardClosedHandler);

If you're using previewer or composer cards, you don't usually need to handle a response. You should design your previewer and composer cards so that any response is optional.

Close a card

An app can request to close a card that it imported. As a result, the card is transitioned off of the screen. When a user closes the card, both the app and the card are notified. However, if the card is closed by the app itself, the card cannot provide a response. Also, when an app closes a card, it closes the entire stack of cards (including children of the card) above it.

Here's how your app can request to close a child card:

blackberry.invoke.closeChildCard();

To learn more about APIs that support card invocation and read code samples, see card.

Last modified: 2014-12-04



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus