Skip to content
This repository has been archived by the owner on Apr 2, 2019. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wyuenho committed Mar 28, 2013
2 parents c67c843 + 840d973 commit ba886c2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/backgrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,14 +757,25 @@ var UriCell = Backgrid.UriCell = Cell.extend({
/** @property */
className: "uri-cell",

// Override this to change the link display text (the text contents
// of the `<a>` element). The function has access to the current row
// via `this.model`.
displayText: function () {
return undefined;
},

render: function () {
this.$el.empty();
var formattedValue = this.formatter.fromRaw(this.model.get(this.column.get("name")));
var displayText = this.displayText();
if (_.isNull(displayText) || _.isUndefined(displayText)) {
displayText = formattedValue;
}
this.$el.append($("<a>", {
href: formattedValue,
title: formattedValue,
target: "_blank"
}).text(formattedValue));
}).text(displayText));
this.delegateEvents();
return this;
}
Expand Down
42 changes: 42 additions & 0 deletions test/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,48 @@ describe("A UriCell", function () {

});

describe("A UriCell with custom display text", function () {

var model;
var column;
var cell;

UriCellWithDisplayText = Backgrid.UriCell.extend({
displayText: function() {
return this.model.get('displayText');
}
});

beforeEach(function () {
model = new Backbone.Model({
url: "http://www.example.com",
displayText: "example display text with a \u0a85 Gujarati character"
});

column = {
name: "url",
cell: UriCellWithDisplayText
};

cell = new UriCellWithDisplayText({
model: model,
column: column
});
});

it("applies a uri-cell class to the cell", function () {
cell.render();
expect(cell.$el.hasClass("uri-cell")).toBe(true);
});

it("renders the model value in an anchor", function () {
cell.render();
expect(cell.$el.find("a").attr("href")).toBe("http://www.example.com");
expect(cell.$el.find("a").text()).toBe("example display text with a \u0a85 Gujarati character");
});

});

describe("An EmailCell", function () {

var model;
Expand Down

0 comments on commit ba886c2

Please sign in to comment.