vendredi 10 novembre 2017

How to get the data object from a single record in the model?

Recently this became a bug in my application, although I don't know what changed to make it so (I haven't upgraded the version of Ember, which is still 1.13). What I need to find out is how to access the object of a single record on the model in the conventional way.

I have the following code to filter my model based on two other properties:

  recordsBySelectedShapeAndColor = get(this, "model").filter(function(rec) {
    //filter the model by the chosen shape and color
    return (
      get(rec, "shape") === theShape &&
      get(rec, "color") === theColor
    );
  });

I then need to create a summary of those filtered records, which I'm using reduce() for, but if that filter returns only one record, then reduce doesn't return the right results, so I have the following condition:

if (recordsBySelectedShapeAndColor.length < 2) {
  summary = recordsBySelectedShapeAndColor[0]._data;
} else {
  summary = recordsBySelectedShapeAndColor.reduce(function(a, b) {
...
}

It's the line within the if that is no longer returning the a simple object, so I changed it to summary = recordsBySelectedShapeAndColor[0]._internalModel._data; and it works, but it seems fishy (._data always did too). Is it code smell to be accessing underscored properties? If so, how can I get just the data from that single record on the model?




Aucun commentaire:

Enregistrer un commentaire