1
votes

I often have some kind of master-detail situation where i try to use a single model for both master and detail view.

The detail page is bound directly to an element in the list with .bindElement(path) when you select an item in the list. The path is available from the binding context. Everyone is happy:

//click handler for list item:
var context = this.getBindingContext();
oDetailPage.bindElement(context.getPath());
oApp.toDetail(oDetailPage);

The challenge is when the list page has an "add" button. I create a new object and put that into the model. But how do I find the path? i have no context:

//click handler for "add" button
var newStuff = {
  propA: "foo",
  propB: 13
};
oModel.setData(oModel.getData().concat(newStuff));
oDetailPage.bindElement(/* What??? */);
oApp.toDetail(oDetailPage);

I've searched for a .findPath(newStuff) method but no such thing exists

2

2 Answers

2
votes

Hi, unfortunatly I am unable to test this but I think this should work

The binding path is pretty much pointer to a specific entry in a collection. The path value does depend on a couple of variables, such as how your model is structured.

For example, if the Model data looks like this :

{"results" : [
            {propA: "foo",
             propB: 13
            },
            {propA: "ber",
             propB: 14
            }]

}

And you concat

{propA: "newItem",
 propB: 15
}

I believe you binding path would be

"/results/2"

You can also, find the most recent index with something like

this.getModel("yourModel").getObject("results").length;

Edit - Unless I miss understand your quesitons, your new entry should be at the end of the model.

1
votes

here are 2 cases

//click handler for "add" button
var newStuff = {
  propA: "foo",
  propB: 13
};

// case 1: root of model data is an array
var oData = oModel.getProperty("/");
oData.push(newStuff);
oDetailPage.bindElement("/" + oData.length - 1);

// case 2: we appends to /results
var oData = oModel.getProperty("/results");
oData.push(newStuff);
oDetailPage.bindElement("/results/" + oData.length - 1);