- Which of the following do you prefer?
- Which is easier to read?
- Which is easier to write?
- Which is easier to maintain?
var dataItem = this.target().closest(".k-grid").data("kendoGrid").dataSource.getByUid(this.target().closest("tr").data("uid"));
Or…
var dataItem = this.target() .closest(".k-grid") .data("kendoGrid") .dataSource .getByUid(this.target().closest("tr").data("uid"));
Or…
var grid = this.target().closest(".k-grid").data("kendoGrid"); var dataSrc = grid.dataSource; var dataItem = dataSrc.getByUid(this.target().closest("tr").data("uid"));
I’m not convinced the answers are obvious.
I’m not a Kendo user, but along the lines of what Mark Rendle suggested, It looks like we need to do something like:
var target = this.target();
if(target){
return $(target).getDataItem();
}else{ return null; }
where the specifics of how to do that would be encapsulated in getDataItem() and have a lot more null-reference checking than we have in the sample 🙂
They’re all pretty bad.
I’d be looking to wrap it all in a jQuery extension/plug-in so I could just call:
this.closestKendoDataItem();
Or, y’know, this.getDataItem();
To me, 2 is the easier to read. To read 3 you have to keep in memory the lines to fully understand what is happening.
And since we are talking about javascript, the less code, the better (I know this would eventually be minified but we still save some bytes with 1 or 2).